From 53cb4c06c2809aee604beaa3bc0fa68a7d81ee63 Mon Sep 17 00:00:00 2001 From: Gusted Date: Tue, 3 Aug 2021 20:30:55 +0200 Subject: [PATCH] refactor(errors): use `errors.is` to account wrapped errors --- cmd/maddy-shadow-helper/main.go | 5 +++-- framework/future/future_test.go | 2 +- internal/auth/shadow/module.go | 6 ++++-- internal/auth/shadow/verify.go | 2 +- internal/check/dnsbl/dnsbl_test.go | 4 ++-- internal/endpoint/openmetrics/om.go | 3 ++- internal/endpoint/smtp/session.go | 4 ++-- internal/storage/imapsql/imapsql.go | 4 ++-- systemd.go | 4 ++-- 9 files changed, 19 insertions(+), 15 deletions(-) diff --git a/cmd/maddy-shadow-helper/main.go b/cmd/maddy-shadow-helper/main.go index 4b997bb6..0c6bcd5d 100644 --- a/cmd/maddy-shadow-helper/main.go +++ b/cmd/maddy-shadow-helper/main.go @@ -20,6 +20,7 @@ package main import ( "bufio" + "errors" "fmt" "os" @@ -43,7 +44,7 @@ func main() { ent, err := shadow.Lookup(username) if err != nil { - if err == shadow.ErrNoSuchUser { + if errors.Is(err, shadow.ErrNoSuchUser) { os.Exit(1) } fmt.Fprintln(os.Stderr, err) @@ -61,7 +62,7 @@ func main() { } if err := ent.VerifyPassword(password); err != nil { - if err == shadow.ErrWrongPassword { + if errors.Is(err, shadow.ErrWrongPassword) { os.Exit(1) } fmt.Fprintln(os.Stderr, err) diff --git a/framework/future/future_test.go b/framework/future/future_test.go index 7850a1d5..aa0d581f 100644 --- a/framework/future/future_test.go +++ b/framework/future/future_test.go @@ -69,7 +69,7 @@ func TestFuture_WaitCtx(t *testing.T) { ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond) defer cancel() _, err := f.GetContext(ctx) - if err != context.DeadlineExceeded { + if !errors.Is(err, context.DeadlineExceeded) { t.Fatal("context is not cancelled") } } diff --git a/internal/auth/shadow/module.go b/internal/auth/shadow/module.go index f669d214..5e4a014c 100644 --- a/internal/auth/shadow/module.go +++ b/internal/auth/shadow/module.go @@ -91,7 +91,9 @@ func (a *Auth) Lookup(username string) (string, bool, error) { ent, err := Lookup(username) if err != nil { - return "", false, nil + if errors.Is(err, ErrNoSuchUser) { + return "", false, nil + } } if !ent.IsAccountValid() { @@ -120,7 +122,7 @@ func (a *Auth) AuthPlain(username, password string) error { } if err := ent.VerifyPassword(password); err != nil { - if err == ErrWrongPassword { + if errors.Is(err, ErrWrongPassword) { return module.ErrUnknownCredentials } return err diff --git a/internal/auth/shadow/verify.go b/internal/auth/shadow/verify.go index 37e09910..19d9c9ea 100644 --- a/internal/auth/shadow/verify.go +++ b/internal/auth/shadow/verify.go @@ -67,7 +67,7 @@ func (e *Entry) VerifyPassword(pass string) (err error) { }() if err := crypt.NewFromHash(e.Pass).Verify(e.Pass, []byte(pass)); err != nil { - if err == crypt.ErrKeyMismatch { + if errors.Is(err, crypt.ErrKeyMismatch) { return ErrWrongPassword } return err diff --git a/internal/check/dnsbl/dnsbl_test.go b/internal/check/dnsbl/dnsbl_test.go index 39dc3260..1845aeb7 100644 --- a/internal/check/dnsbl/dnsbl_test.go +++ b/internal/check/dnsbl/dnsbl_test.go @@ -20,8 +20,8 @@ package dnsbl import ( "context" + "errors" "net" - "reflect" "testing" "github.com/foxcpp/go-mockdns" @@ -35,7 +35,7 @@ func TestCheckList(t *testing.T) { log: testutils.Logger(t, "dnsbl"), } err := mod.checkList(context.Background(), cfg, ip, ehlo, mailFrom) - if !reflect.DeepEqual(err, expectedErr) { + if !errors.Is(err, expectedErr) { t.Errorf("expected err to be '%#v', got '%#v'", expectedErr, err) } } diff --git a/internal/endpoint/openmetrics/om.go b/internal/endpoint/openmetrics/om.go index 83d8be98..4e945243 100644 --- a/internal/endpoint/openmetrics/om.go +++ b/internal/endpoint/openmetrics/om.go @@ -19,6 +19,7 @@ along with this program. If not, see . package openmetrics import ( + "errors" "fmt" "net" "net/http" @@ -76,7 +77,7 @@ func (e *Endpoint) Init(cfg *config.Map) error { go func() { e.logger.Println("listening on", endp.String()) err := e.serv.Serve(l) - if err != nil && err != http.ErrServerClosed { + if err != nil && !errors.Is(err, http.ErrServerClosed) { e.logger.Error("serve failed", err, "endpoint", a) } }() diff --git a/internal/endpoint/smtp/session.go b/internal/endpoint/smtp/session.go index 4d7f4f03..6a11978f 100644 --- a/internal/endpoint/smtp/session.go +++ b/internal/endpoint/smtp/session.go @@ -236,7 +236,7 @@ func (s *Session) Mail(from string, opts smtp.MailOptions) error { // Will initialize s.msgCtx. msgID, err := s.startDelivery(s.sessionCtx, from, opts) if err != nil { - if err != context.DeadlineExceeded { + if !errors.Is(err, context.DeadlineExceeded) { s.log.Error("MAIL FROM error", err, "msg_id", msgID) } return s.endp.wrapErr(msgID, !opts.UTF8, "MAIL", err) @@ -300,7 +300,7 @@ func (s *Session) Rcpt(to string) error { // It will initialize s.msgCtx. msgID, err := s.startDelivery(s.sessionCtx, s.mailFrom, s.opts) if err != nil { - if err != context.DeadlineExceeded { + if !errors.Is(err, context.DeadlineExceeded) { s.log.Error("MAIL FROM error (deferred)", err, "rcpt", to, "msg_id", msgID) } s.deliveryErr = s.endp.wrapErr(msgID, !s.opts.UTF8, "RCPT", err) diff --git a/internal/storage/imapsql/imapsql.go b/internal/storage/imapsql/imapsql.go index 35adf7f2..d5fb1879 100644 --- a/internal/storage/imapsql/imapsql.go +++ b/internal/storage/imapsql/imapsql.go @@ -379,12 +379,12 @@ func (store *Storage) GetOrCreateIMAPAcct(username string) (backend.User, error) func (store *Storage) Lookup(ctx context.Context, key string) (string, bool, error) { accountName, err := store.authNormalize(ctx, key) if err != nil { - return "", false, nil + return "", false, err } usr, err := store.Back.GetUser(accountName) if err != nil { - if err == imapsql.ErrUserDoesntExists { + if errors.Is(err, imapsql.ErrUserDoesntExists) { return "", false, nil } return "", false, err diff --git a/systemd.go b/systemd.go index f6142f8f..429942ac 100644 --- a/systemd.go +++ b/systemd.go @@ -80,7 +80,7 @@ func setScmPassCred(sock *net.UnixConn) error { func systemdStatus(status SDStatus, desc string) { sock, err := sdNotifySock() if err != nil { - if err != ErrNoNotifySock { + if !errors.Is(err, ErrNoNotifySock) { log.Println("systemd: failed to acquire notify socket:", err) } return @@ -107,7 +107,7 @@ func systemdStatus(status SDStatus, desc string) { func systemdStatusErr(reportedErr error) { sock, err := sdNotifySock() if err != nil { - if err != ErrNoNotifySock { + if !errors.Is(err, ErrNoNotifySock) { log.Println("systemd: failed to acquire notify socket:", err) } return