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 and fmt package to join and wrap errors #460

Closed
wants to merge 4 commits into from
Closed

Use errors and fmt package to join and wrap errors #460

wants to merge 4 commits into from

Conversation

ghost
Copy link

@ghost ghost commented Jul 31, 2024

Good day!

First of thank you for maintaining and providing the watermill package!

I opened this PR because I noticed that the hashicorp/go-multierror and github.com/pkg/errors where added as an indirect dependency when using watermill.
As these dependency have native alternatives I created this PR.

Since golang 1.20 the go errors package supports errors.Join which replaces the need for the hashicorp/go-multierror package which was providing the same functionality.
To wrap errors fmt.Errorf with the %w format specifier (introduced in golang 1.13) is now used instead of errors.Wrap which was provided by the github.com/pkg/errors package.

Please let me know what you think and if any changes are needed.
Have a great day and thank you for reviewing this PR!

Cheers,
Warnar

This PR resolves #447

Copy link

@ccoVeille ccoVeille left a comment

Choose a reason for hiding this comment

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

A few suggestions.

But don't get me wrong about the number of comments I posted, I love your changes

"flag"
"fmt"
"io/ioutil"

Choose a reason for hiding this comment

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

Side remark as you are upgrading code to use go 1.13 wrapping

io/ioutil is deprecate

Maybe something could be done too. Not necessarily in this PR, it's up to you

Copy link
Author

Choose a reason for hiding this comment

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

Good spot.
I added the replacements in 3dc9d72

Comment on lines 5 to 6
"errors"
stdErrors "errors"

Choose a reason for hiding this comment

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

Nope, we are importing twice.

It's a side effect of previous usage of two packages for errors. But now, you should clean

Please replace all the stdErrors. to errors.Is

@@ -1,11 +1,10 @@
package cqrs

import (
"errors"
stdErrors "errors"

Choose a reason for hiding this comment

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

@@ -2,11 +2,12 @@ package cqrs

import (
"context"
"errors"
stdErrors "errors"

Choose a reason for hiding this comment

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

Same

Comment on lines 5 to 9
"fmt"
"testing"

"errors"
"github.com/ThreeDotsLabs/watermill/components/cqrs"

Choose a reason for hiding this comment

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

Suggested change
"fmt"
"testing"
"errors"
"github.com/ThreeDotsLabs/watermill/components/cqrs"
"fmt"
"errors"
"testing"
"github.com/ThreeDotsLabs/watermill/components/cqrs"

@@ -9,8 +9,8 @@ import (

"github.com/stretchr/testify/assert"

"errors"

Choose a reason for hiding this comment

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

Same

@@ -5,7 +5,7 @@ import (
"testing"
"time"

"github.com/pkg/errors"
"errors"

Choose a reason for hiding this comment

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

Same

@@ -7,7 +7,7 @@ import (
"testing"
"time"

"github.com/pkg/errors"
"errors"

Choose a reason for hiding this comment

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

Same

@@ -4,8 +4,8 @@ import (
"context"
"sync"

"errors"

Choose a reason for hiding this comment

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

Same

tools/mill/cmd/googlecloud.go Show resolved Hide resolved
Warnar Boekkooi added 2 commits August 1, 2024 17:22
As of go 1.20 the go `errors` package supports `errors.Join` which replaces the need for the `hashicorp/go-multierror` package which was providing the same functionality.
To wrap errors `fmt.Errorf` with the `%w` format specifier (introduced in golang 1.13) is now used instead of `errors.Wrap` which was provided by the `github.com/pkg/errors` package.
Remove the usage of ioutil which was deprecated in golang 1.16

See https://go.dev/doc/go1.16#ioutil
@ghost
Copy link
Author

ghost commented Aug 1, 2024

Hey @ccoVeille

Thank you for the code review.
I think I managed to sort all the imports correctly now and also removed ioutil usage.

Let me know if I missed something or if some other change is needed.
Cheers

Comment on lines 15 to 16
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

Choose a reason for hiding this comment

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

I think this block should be before the watermill ones

message/router/middleware/message_test.go Show resolved Hide resolved
Comment on lines 48 to 50
if err != nil {
t.Fatal(errors.Wrap(err, "call to unknown endpoint failed"))
t.Fatal(fmt.Errorf("call to unknown endpoint failed: %w", err))
}

Choose a reason for hiding this comment

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

Not related to your changes

But here I would expect to have

require.NoError(t, err, "call to unknown failed")

@@ -46,7 +46,7 @@ func TestCreateRegistryAndServeHTTP_unknown_endpoint(t *testing.T) {
}

if err != nil {
t.Fatal(errors.Wrap(err, "call to unknown endpoint failed"))
t.Fatal(fmt.Errorf("call to unknown endpoint failed: %w", err))

Choose a reason for hiding this comment

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

This is a fatalf

Suggested change
t.Fatal(fmt.Errorf("call to unknown endpoint failed: %w", err))
t.Fatalf("call to unknown endpoint failed: %v", err)

t.Fatal(errors.Wrap(err, "registration of prometheus build info collector failed"))
t.Fatal(fmt.Errorf("registration of prometheus build info collector failed: %w", err))

Choose a reason for hiding this comment

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

Here the previous code is somehow bad.

And you are changing it to something better, but unfortunately still bad.

See

Comment on lines 28 to 30
if err != nil {
t.Fatal(errors.Wrap(err, "call to metrics endpoint failed"))
t.Fatal(fmt.Errorf("call to metrics endpoint failed: %w", err))
}

Choose a reason for hiding this comment

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

Same either Fatalf, or require.NoError

@ghost
Copy link
Author

ghost commented Aug 2, 2024

Hey @ccoVeille

I added commit bbe870c to remove some t.Fatal and replaced them with testify require.
(I hope) I also fixed all import sort order issues now.

Please have another look and let me know if I missed something (again) 😄

@ccoVeille
Copy link

I can help with adding golangci-lint checkers to avoid such issues and others

@ccoVeille
Copy link

I can help with adding golangci-lint checkers to avoid such issues and others

I opened #463 about it

@@ -26,7 +25,7 @@ func Recoverer(h message.HandlerFunc) message.HandlerFunc {

defer func() {
if r := recover(); r != nil || panicked {
err = errors.WithStack(RecoveredPanicError{V: r, Stacktrace: string(debug.Stack())})
Copy link
Member

Choose a reason for hiding this comment

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

This might be a bit of a breaking change, actually. 🤔 Perhaps we could somehow keep the stack trace here?

Copy link
Author

Choose a reason for hiding this comment

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

I'm not sure how this really is a breaking change or not.
A debug.Stack call is used to get the stacktrace into RecoveredPanicError. (via a call to runtime.Callers).
This seems to me like the same function that errors.WithStack provides as it's using runtime.Callers and then formatting it.

Now, should someone use reflection or have an interface to represent and error from the errors package then yes this PR could break things.
Personally, I feel this change is not breaking the contract of the library as it's still returning the error interface.

@ghost ghost closed this by deleting the head repository Aug 29, 2024
@ccoVeille
Copy link

I'm unsure why the PR was closed.

The code was OK. Is it because of the comment here?

#460 (comment)

This was a small part of your changes.

Even if there were a discussion about it, most of the code was OK to merge, no?

@m110
Copy link
Member

m110 commented Sep 1, 2024

@ccoVeille I agree. I'm not sure about the comment yet (I'm reviewing lots of Watermill PRs now, so I need some time), but the rest of the good looks good to me.

@m110
Copy link
Member

m110 commented Sep 1, 2024

Oh, I think the OP's account got deleted.

@ccoVeille
Copy link

Oh OK. Then you can consider to reopen the PR.

I can do it of course, but with some delay maybe.

I hope the commits will stay live for a few days

@m110
Copy link
Member

m110 commented Sep 1, 2024

Sadly, it seems we can't reopen it, as the fork is gone. 🫨

@ccoVeille
Copy link

Are you able to merge? You could revert then.

Quite dirty, but why not

@ccoVeille
Copy link

You can use this as a maintainer maybe

https://github.blog/news-insights/product-news/change-the-base-branch-of-a-pull-request/

this would prefer to merge in master, you will have then a branch and you will be able to open a PR for it

This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Removing usage of https://github.com/hashicorp/go-multierror
2 participants