Skip to content

Commit

Permalink
refactor(errors): use errors.is to account wrapped errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Gusted authored and foxcpp committed Aug 10, 2021
1 parent e4fb72e commit 53cb4c0
Show file tree
Hide file tree
Showing 9 changed files with 19 additions and 15 deletions.
5 changes: 3 additions & 2 deletions cmd/maddy-shadow-helper/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package main

import (
"bufio"
"errors"
"fmt"
"os"

Expand All @@ -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)
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion framework/future/future_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
6 changes: 4 additions & 2 deletions internal/auth/shadow/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion internal/auth/shadow/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions internal/check/dnsbl/dnsbl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ package dnsbl

import (
"context"
"errors"
"net"
"reflect"
"testing"

"github.com/foxcpp/go-mockdns"
Expand All @@ -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)
}
}
Expand Down
3 changes: 2 additions & 1 deletion internal/endpoint/openmetrics/om.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
package openmetrics

import (
"errors"
"fmt"
"net"
"net/http"
Expand Down Expand Up @@ -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)
}
}()
Expand Down
4 changes: 2 additions & 2 deletions internal/endpoint/smtp/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions internal/storage/imapsql/imapsql.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions systemd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 53cb4c0

Please sign in to comment.