Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use errors.Is for error comparison instead of direct equality checks #502

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion components/metrics/http.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package metrics

import (
"errors"
"net/http"

"github.com/go-chi/chi/v5"
Expand Down Expand Up @@ -31,7 +32,7 @@ func ServeHTTP(addr string, registry *prometheus.Registry) (cancel func()) {

go func() {
err := server.ListenAndServe()
if err != http.ErrServerClosed {
if !errors.Is(err, http.ErrServerClosed) {
panic(err)
}
}()
Expand Down
3 changes: 2 additions & 1 deletion log.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package watermill

import (
"errors"
"fmt"
"io"
"log"
Expand Down Expand Up @@ -216,7 +217,7 @@ func (c *CaptureLoggerAdapter) HasError(err error) bool {
defer c.lock.Unlock()

for _, capturedMsg := range c.captured[ErrorLogLevel] {
if capturedMsg.Err == err {
if errors.Is(err, capturedMsg.Err) {
return true
}
}
Expand Down
19 changes: 9 additions & 10 deletions message/router/middleware/poison_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,20 @@ package middleware_test

import (
"context"
stdErrors "errors"
"testing"
"time"

"github.com/ThreeDotsLabs/watermill"
"github.com/ThreeDotsLabs/watermill/message/subscriber"
"github.com/ThreeDotsLabs/watermill/pubsub/gochannel"

"github.com/hashicorp/go-multierror"

"github.com/ThreeDotsLabs/watermill/message"

"github.com/ThreeDotsLabs/watermill/message/router/middleware"
"github.com/pkg/errors"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont see a need for this deprecated one

Remove it, so you can use errors directly without an alias

The call to errors.Wrap can be replaced by fmt.Errorf("%w ...")

errors.New can be used directly from stdlib

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like errors.Cause is being used in the current implementation, which makes it a bit more challenging to simply remove the alias.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From what I had seen before raising my point, errors.Cause is used only in the code, not in the test file I quoted.

But, I have missed something. And I was wrong

There is this indeed.

assert.Equal(t, errFailed, errors.Cause(multierr.WrappedErrors()[1]))

this could be rewritten to use maybe

assert.ErrorIs(t, multierr, errFailed)

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/ThreeDotsLabs/watermill"
"github.com/ThreeDotsLabs/watermill/message"
"github.com/ThreeDotsLabs/watermill/message/router/middleware"
"github.com/ThreeDotsLabs/watermill/message/subscriber"
"github.com/ThreeDotsLabs/watermill/pubsub/gochannel"
)

const topic = "testing_poison_queue_topic"
Expand Down Expand Up @@ -213,7 +212,7 @@ func TestPoisonQueueWithFilter_poison_queue(t *testing.T) {
msg := message.NewMessage("uuid", []byte("payload"))

poisonQueue, err := middleware.PoisonQueueWithFilter(&poisonPublisher, topic, func(err error) bool {
return err == poisonQueueErr
return stdErrors.Is(err, poisonQueueErr)
})
require.NoError(t, err)

Expand All @@ -232,7 +231,7 @@ func TestPoisonQueueWithFilter_non_poison_queue(t *testing.T) {
msg := message.NewMessage("uuid", []byte("payload"))

poisonQueue, err := middleware.PoisonQueueWithFilter(&poisonPublisher, topic, func(err error) bool {
return err != nonPoisonQueueErr
return !stdErrors.Is(err, nonPoisonQueueErr)
})
require.NoError(t, err)

Expand Down