fix(errors): put exported errors in util
continuous-integration/drone/push Build is passing Details
continuous-integration/drone/pr Build is passing Details

IMO it is better to centralize them

Signed-off-by: Sam Therapy <sam@samtherapy.net>
This commit is contained in:
Sam Therapy 2022-12-06 20:46:04 +01:00
parent 45acd03dff
commit 4ae8471580
Signed by: sam
GPG Key ID: 4D8B07C18F31ACBD
6 changed files with 26 additions and 19 deletions

View File

@ -195,7 +195,7 @@ func ParseCLI(args []string, version string) (*util.Options, error) {
if *versionFlag {
fmt.Printf("awl version %s, built with %s\n", version, runtime.Version())
return &opts, ErrNotError
return &opts, util.ErrNotError
}
// Parse all the arguments that don't start with - or --
@ -233,9 +233,6 @@ func ParseCLI(args []string, version string) (*util.Options, error) {
return &opts, nil
}
// ErrNotError is for returning not error.
var ErrNotError = errors.New("not an error")
var errNoArg = errors.New("no argument given")
type errInvalidArg struct {

View File

@ -7,6 +7,7 @@ import (
"time"
cli "git.froth.zone/sam/awl/cmd"
"git.froth.zone/sam/awl/pkg/util"
"gotest.tools/v3/assert"
)
@ -103,7 +104,7 @@ func TestVersion(t *testing.T) {
_, err := cli.ParseCLI(args, "test")
assert.ErrorType(t, err, cli.ErrNotError)
assert.ErrorType(t, err, util.ErrNotError)
}
func TestTimeout(t *testing.T) {

View File

@ -18,7 +18,7 @@ var version = "DEV"
func main() {
if opts, code, err := run(os.Args); err != nil {
// TODO: Make not ew
if errors.Is(err, cli.ErrNotError) || strings.Contains(err.Error(), "help requested") {
if errors.Is(err, util.ErrNotError) || strings.Contains(err.Error(), "help requested") {
os.Exit(0)
} else {
opts.Logger.Error(err)

View File

@ -72,7 +72,7 @@ func (resolver *HTTPSResolver) LookUp(msg *dns.Msg) (util.Response, error) {
}
if res.StatusCode != http.StatusOK {
return util.Response{}, &ErrHTTPStatus{res.StatusCode}
return util.Response{}, &util.ErrHTTPStatus{Code: res.StatusCode}
}
resolver.opts.Logger.Debug("https: reading response")
@ -98,12 +98,3 @@ func (resolver *HTTPSResolver) LookUp(msg *dns.Msg) (util.Response, error) {
return resp, nil
}
// ErrHTTPStatus is returned when DoH returns a bad status code.
type ErrHTTPStatus struct {
code int
}
func (e *ErrHTTPStatus) Error() string {
return fmt.Sprintf("doh server responded with HTTP %d", e.code)
}

View File

@ -7,7 +7,6 @@ import (
"testing"
"git.froth.zone/sam/awl/pkg/query"
"git.froth.zone/sam/awl/pkg/resolvers"
"git.froth.zone/sam/awl/pkg/util"
"github.com/miekg/dns"
"gotest.tools/v3/assert"
@ -84,7 +83,7 @@ func TestHTTPS(t *testing.T) {
)
for i := 0; i <= test.opts.Request.Retries; i++ {
res, err = query.CreateQuery(test.opts)
if err == nil || errors.Is(err, &resolvers.ErrHTTPStatus{}) {
if err == nil || errors.Is(err, &util.ErrHTTPStatus{}) {
break
}
}
@ -93,7 +92,7 @@ func TestHTTPS(t *testing.T) {
assert.NilError(t, err)
assert.Assert(t, res != util.Response{})
} else {
if errors.Is(err, &resolvers.ErrHTTPStatus{}) {
if errors.Is(err, &util.ErrHTTPStatus{}) {
assert.ErrorContains(t, err, "404")
}
assert.Equal(t, res, util.Response{})

19
pkg/util/errors.go Normal file
View File

@ -0,0 +1,19 @@
package util
import (
"errors"
"fmt"
)
// ErrHTTPStatus is returned when DoH returns a bad status code.
type ErrHTTPStatus struct {
// Status code
Code int
}
func (e *ErrHTTPStatus) Error() string {
return fmt.Sprintf("doh server responded with HTTP %d", e.Code)
}
// ErrNotError is an error that is not actually an error.
var ErrNotError = errors.New("not an error")