From 64768f095600992b1170b44deaa9afb48169069f Mon Sep 17 00:00:00 2001 From: Sam Therapy Date: Tue, 6 Dec 2022 21:39:28 +0000 Subject: [PATCH] fix(errors): put exported errors in util (#161) Centralize them in one place Reviewed-on: https://git.froth.zone/sam/awl/pulls/161 Reviewed-by: grumbulon --- cmd/cli.go | 5 +---- cmd/cli_test.go | 3 ++- main.go | 2 +- pkg/resolvers/HTTPS.go | 11 +---------- pkg/resolvers/HTTPS_test.go | 5 ++--- pkg/util/errors.go | 19 +++++++++++++++++++ 6 files changed, 26 insertions(+), 19 deletions(-) create mode 100644 pkg/util/errors.go diff --git a/cmd/cli.go b/cmd/cli.go index d1affb2..62bfe76 100644 --- a/cmd/cli.go +++ b/cmd/cli.go @@ -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 { diff --git a/cmd/cli_test.go b/cmd/cli_test.go index e18d7ee..3892b62 100644 --- a/cmd/cli_test.go +++ b/cmd/cli_test.go @@ -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) { diff --git a/main.go b/main.go index 37a3c8d..987bb39 100644 --- a/main.go +++ b/main.go @@ -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) diff --git a/pkg/resolvers/HTTPS.go b/pkg/resolvers/HTTPS.go index 9cfa691..bfe1660 100644 --- a/pkg/resolvers/HTTPS.go +++ b/pkg/resolvers/HTTPS.go @@ -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) -} diff --git a/pkg/resolvers/HTTPS_test.go b/pkg/resolvers/HTTPS_test.go index 75d5d95..2fea143 100644 --- a/pkg/resolvers/HTTPS_test.go +++ b/pkg/resolvers/HTTPS_test.go @@ -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{}) diff --git a/pkg/util/errors.go b/pkg/util/errors.go new file mode 100644 index 0000000..d37a6ba --- /dev/null +++ b/pkg/util/errors.go @@ -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")