Change errors to use xerrors package.
This commit is contained in:
@@ -1,41 +0,0 @@
|
||||
package errors
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/antanst/go_errors"
|
||||
)
|
||||
|
||||
// HostError is an error encountered while
|
||||
// visiting a host, and should be recorded
|
||||
// to the snapshot.
|
||||
type HostError struct {
|
||||
Err error
|
||||
}
|
||||
|
||||
func (e *HostError) Error() string {
|
||||
return e.Err.Error()
|
||||
}
|
||||
|
||||
func (e *HostError) Unwrap() error {
|
||||
return e.Err
|
||||
}
|
||||
|
||||
func NewHostError(err error) error {
|
||||
return &HostError{Err: err}
|
||||
}
|
||||
|
||||
func IsHostError(err error) bool {
|
||||
if err == nil {
|
||||
return false
|
||||
}
|
||||
var asError *HostError
|
||||
return go_errors.As(err, &asError)
|
||||
}
|
||||
|
||||
// Sentinel errors used for their string message primarily.
|
||||
// Do not use them by themselves, to be embedded to HostError.
|
||||
var (
|
||||
ErrBlacklistMatch = fmt.Errorf("black list match")
|
||||
ErrRobotsMatch = fmt.Errorf("robots match")
|
||||
)
|
||||
@@ -1,38 +0,0 @@
|
||||
package errors_test
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"gemini-grc/gemini"
|
||||
)
|
||||
|
||||
func TestErrGemini(t *testing.T) {
|
||||
t.Parallel()
|
||||
err := gemini.NewGeminiError(50, "50 server error")
|
||||
if !errors.As(err, new(*gemini.GeminiError)) {
|
||||
t.Errorf("TestErrGemini fail")
|
||||
}
|
||||
}
|
||||
|
||||
func TestErrGeminiWrapped(t *testing.T) {
|
||||
t.Parallel()
|
||||
err := gemini.NewGeminiError(50, "50 server error")
|
||||
errWrapped := fmt.Errorf("%w wrapped", err)
|
||||
if !errors.As(errWrapped, new(*gemini.GeminiError)) {
|
||||
t.Errorf("TestErrGeminiWrapped fail")
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsGeminiError(t *testing.T) {
|
||||
t.Parallel()
|
||||
err1 := gemini.NewGeminiError(50, "50 server error")
|
||||
if !gemini.IsGeminiError(err1) {
|
||||
t.Errorf("TestGeminiError fail #1")
|
||||
}
|
||||
wrappedErr1 := fmt.Errorf("wrapped %w", err1)
|
||||
if !gemini.IsGeminiError(wrappedErr1) {
|
||||
t.Errorf("TestGeminiError fail #2")
|
||||
}
|
||||
}
|
||||
29
common/errors/hostError.go
Normal file
29
common/errors/hostError.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package commonErrors
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"git.antanst.com/antanst/xerrors"
|
||||
)
|
||||
|
||||
type HostError struct {
|
||||
xerrors.XError
|
||||
}
|
||||
|
||||
func IsHostError(err error) bool {
|
||||
var temp *HostError
|
||||
return errors.As(err, &temp)
|
||||
}
|
||||
|
||||
func NewHostError(err error) error {
|
||||
xerr := xerrors.XError{
|
||||
UserMsg: "",
|
||||
Code: 0,
|
||||
Err: err,
|
||||
IsFatal: false,
|
||||
}
|
||||
|
||||
return &HostError{
|
||||
xerr,
|
||||
}
|
||||
}
|
||||
8
common/errors/sentinelErrors.go
Normal file
8
common/errors/sentinelErrors.go
Normal file
@@ -0,0 +1,8 @@
|
||||
package commonErrors
|
||||
|
||||
import "fmt"
|
||||
|
||||
var (
|
||||
ErrBlacklistMatch = fmt.Errorf("black list match")
|
||||
ErrRobotsMatch = fmt.Errorf("robots match")
|
||||
)
|
||||
Reference in New Issue
Block a user