Skip to content

Commit 53cb4c0

Browse files
Gustedfoxcpp
Gusted
authored andcommittedAug 10, 2021
refactor(errors): use errors.is to account wrapped errors
1 parent e4fb72e commit 53cb4c0

File tree

9 files changed

+19
-15
lines changed

9 files changed

+19
-15
lines changed
 

‎cmd/maddy-shadow-helper/main.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ package main
2020

2121
import (
2222
"bufio"
23+
"errors"
2324
"fmt"
2425
"os"
2526

@@ -43,7 +44,7 @@ func main() {
4344

4445
ent, err := shadow.Lookup(username)
4546
if err != nil {
46-
if err == shadow.ErrNoSuchUser {
47+
if errors.Is(err, shadow.ErrNoSuchUser) {
4748
os.Exit(1)
4849
}
4950
fmt.Fprintln(os.Stderr, err)
@@ -61,7 +62,7 @@ func main() {
6162
}
6263

6364
if err := ent.VerifyPassword(password); err != nil {
64-
if err == shadow.ErrWrongPassword {
65+
if errors.Is(err, shadow.ErrWrongPassword) {
6566
os.Exit(1)
6667
}
6768
fmt.Fprintln(os.Stderr, err)

‎framework/future/future_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func TestFuture_WaitCtx(t *testing.T) {
6969
ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond)
7070
defer cancel()
7171
_, err := f.GetContext(ctx)
72-
if err != context.DeadlineExceeded {
72+
if !errors.Is(err, context.DeadlineExceeded) {
7373
t.Fatal("context is not cancelled")
7474
}
7575
}

‎internal/auth/shadow/module.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,9 @@ func (a *Auth) Lookup(username string) (string, bool, error) {
9191

9292
ent, err := Lookup(username)
9393
if err != nil {
94-
return "", false, nil
94+
if errors.Is(err, ErrNoSuchUser) {
95+
return "", false, nil
96+
}
9597
}
9698

9799
if !ent.IsAccountValid() {
@@ -120,7 +122,7 @@ func (a *Auth) AuthPlain(username, password string) error {
120122
}
121123

122124
if err := ent.VerifyPassword(password); err != nil {
123-
if err == ErrWrongPassword {
125+
if errors.Is(err, ErrWrongPassword) {
124126
return module.ErrUnknownCredentials
125127
}
126128
return err

‎internal/auth/shadow/verify.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func (e *Entry) VerifyPassword(pass string) (err error) {
6767
}()
6868

6969
if err := crypt.NewFromHash(e.Pass).Verify(e.Pass, []byte(pass)); err != nil {
70-
if err == crypt.ErrKeyMismatch {
70+
if errors.Is(err, crypt.ErrKeyMismatch) {
7171
return ErrWrongPassword
7272
}
7373
return err

‎internal/check/dnsbl/dnsbl_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ package dnsbl
2020

2121
import (
2222
"context"
23+
"errors"
2324
"net"
24-
"reflect"
2525
"testing"
2626

2727
"github.com/foxcpp/go-mockdns"
@@ -35,7 +35,7 @@ func TestCheckList(t *testing.T) {
3535
log: testutils.Logger(t, "dnsbl"),
3636
}
3737
err := mod.checkList(context.Background(), cfg, ip, ehlo, mailFrom)
38-
if !reflect.DeepEqual(err, expectedErr) {
38+
if !errors.Is(err, expectedErr) {
3939
t.Errorf("expected err to be '%#v', got '%#v'", expectedErr, err)
4040
}
4141
}

‎internal/endpoint/openmetrics/om.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
1919
package openmetrics
2020

2121
import (
22+
"errors"
2223
"fmt"
2324
"net"
2425
"net/http"
@@ -76,7 +77,7 @@ func (e *Endpoint) Init(cfg *config.Map) error {
7677
go func() {
7778
e.logger.Println("listening on", endp.String())
7879
err := e.serv.Serve(l)
79-
if err != nil && err != http.ErrServerClosed {
80+
if err != nil && !errors.Is(err, http.ErrServerClosed) {
8081
e.logger.Error("serve failed", err, "endpoint", a)
8182
}
8283
}()

‎internal/endpoint/smtp/session.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ func (s *Session) Mail(from string, opts smtp.MailOptions) error {
236236
// Will initialize s.msgCtx.
237237
msgID, err := s.startDelivery(s.sessionCtx, from, opts)
238238
if err != nil {
239-
if err != context.DeadlineExceeded {
239+
if !errors.Is(err, context.DeadlineExceeded) {
240240
s.log.Error("MAIL FROM error", err, "msg_id", msgID)
241241
}
242242
return s.endp.wrapErr(msgID, !opts.UTF8, "MAIL", err)
@@ -300,7 +300,7 @@ func (s *Session) Rcpt(to string) error {
300300
// It will initialize s.msgCtx.
301301
msgID, err := s.startDelivery(s.sessionCtx, s.mailFrom, s.opts)
302302
if err != nil {
303-
if err != context.DeadlineExceeded {
303+
if !errors.Is(err, context.DeadlineExceeded) {
304304
s.log.Error("MAIL FROM error (deferred)", err, "rcpt", to, "msg_id", msgID)
305305
}
306306
s.deliveryErr = s.endp.wrapErr(msgID, !s.opts.UTF8, "RCPT", err)

‎internal/storage/imapsql/imapsql.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -379,12 +379,12 @@ func (store *Storage) GetOrCreateIMAPAcct(username string) (backend.User, error)
379379
func (store *Storage) Lookup(ctx context.Context, key string) (string, bool, error) {
380380
accountName, err := store.authNormalize(ctx, key)
381381
if err != nil {
382-
return "", false, nil
382+
return "", false, err
383383
}
384384

385385
usr, err := store.Back.GetUser(accountName)
386386
if err != nil {
387-
if err == imapsql.ErrUserDoesntExists {
387+
if errors.Is(err, imapsql.ErrUserDoesntExists) {
388388
return "", false, nil
389389
}
390390
return "", false, err

‎systemd.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func setScmPassCred(sock *net.UnixConn) error {
8080
func systemdStatus(status SDStatus, desc string) {
8181
sock, err := sdNotifySock()
8282
if err != nil {
83-
if err != ErrNoNotifySock {
83+
if !errors.Is(err, ErrNoNotifySock) {
8484
log.Println("systemd: failed to acquire notify socket:", err)
8585
}
8686
return
@@ -107,7 +107,7 @@ func systemdStatus(status SDStatus, desc string) {
107107
func systemdStatusErr(reportedErr error) {
108108
sock, err := sdNotifySock()
109109
if err != nil {
110-
if err != ErrNoNotifySock {
110+
if !errors.Is(err, ErrNoNotifySock) {
111111
log.Println("systemd: failed to acquire notify socket:", err)
112112
}
113113
return

0 commit comments

Comments
 (0)
Please sign in to comment.