-
Notifications
You must be signed in to change notification settings - Fork 700
Conversation
Go 1.13 adds support for error chains to the standard libary's errors package. The new standard library functions require an Unwrap method to be provided by an error type. This change adds a new Unwrap method (identical to the existing Cause method) to the unexported error types.
I was about to open exactly the same PR. Thank you! |
I wonder if it makes sense to add unwrapping to the |
that sounds reasonable but perhaps a different PR? |
Well, the title says |
Unwrapping Go 1.13 error chains can be done with the new functions in the standard library, so I don't see a need to support it here. Perhaps I could have named the PR better, but I'm going to leave it as-is unless I hear differently from Dave. |
Existing code using |
IMO this is necessary so that existing codebases can gracefully transition to the stdlib error wrapping without having to go whole-hog changing thousands of lines of code to use UnWrap() rather than Cause. Please take a look, @davecheney |
What about using |
We're forked this library and modify for using internally new package errors on go 1.13, furthermore on if you use a previous version the library was been compiled about xerrors package. If anyone want use it our version, will can find it here: Any feedback is more than welcome! |
We've made a more extensive errors library that's both compatible with |
I created a drop-in replacement for both the stdlib errors package and pkg/errors: https://github.com/emperror/errors |
I believe this PR is very much needed as the Frame capture did not make it through from |
@chuckha You don't really need the change here to start using Go 1.13 errors, I've describe a workaround on my blog: https://mycodesmells.com/post/migrating-pkg-errors-to-go-113-errors 😉 |
Go 1.13 has been released and Once this pull request to be merged, we can use I hope this pull request will be merged soon. |
Thank you for this PR. I'm sorry I have had no time to work on this package since I called for the 1.0 release earlier in the year -- my day job is very demanding. I wasn't expecting that Go 1.13 would ship without stack trace support in its errors. I hope to have time to review this properly in November. |
I think if you add import (
stderrors "errors"
"github.com/pkg/errors"
) to use |
some error variable in std library (example), after go1.13, can not use |
I think we only need |
The I think support only |
@Songmu if pkg/errors don't support |
@Sherlock-Holo I'm agree with you that if the package errors had implemented the functions But in this case I think that this PRs is only for add the compatibility with the standard library, and adding only the |
@aperezg good idea, I will create a PR to add |
I'm really looking forward to this PR getting merged 🙏Nice work everyone! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could you change the name of file from go1.13_compat_test.go to go113_test.go
go1.13_compat_test.go
Outdated
func TestErrorChainCompat(t *testing.T) { | ||
err := stdlib_errors.New("error that gets wrapped") | ||
wrapped := errors.Wrap(err, "wrapped up") | ||
if !stdlib_errors.Is(wrapped, err) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if !stdlib_errors.Is(wrapped, err) { | |
if !errors.Is(wrapped, err) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that is not necessary use the package _test
in this case because you can use directly the std library and check against the package itself
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wanted it to be a black box test, and that is why I put it in a separate package.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok so is a good argument to me, we need to merge this #212 before that we can merge your PR, because in otherwise the travis ci will never pass. But it's look great, and I would like to merge this ASAP :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Once #212 is merged, I will rebase and squash this branch.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do not see the utility here in actually pushing it into a separate package. You can still do blackbox testing even if technically you could look inside the box.
The design is in the design, not in the arbitrarily forcing a semantic. Consider as well all the people forking the project, and now that test is going to pull this package for their tests, and not their own forked repo.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see your point and is very reasonable, but the package itself can't be tested normally from a fork, because have the repository path hardcode in some tests, for example:
https://github.com/pkg/errors/blob/master/format_test.go#L29
So if anyone fork this repository, he/she must be moved under $GOPATH/src/github.com/pkg/errors or they also could use docker instead. Otherwise the test will never pass on local.
Furthermore this can also be resolved using go modules, but yes for now the package haven't this functionality.
But, I agree that in this case one way on another to do this black box test is similar to the other. So maybe would be ok, if we try to avoid more accoplated code with the package itself.
What do you think @puellanivis?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm… fun test. But a critical difference is that the format_test
would break loudly, while this would not even break, because a forking author may not even realize that this import is here, and thus get a false sense of security that their tests are passing, even though they are not even running against their own code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I totally agree that are two different case, this case could produce as you said false sense of security. My point was that with the current state of the package tests, the package couldn't be tested from another directory. But, I see your point.
@jayschwa could you change the test and use the package errors instead the package errors_test? Is truth that Go provide with the package _test to realize the black box test on secure way, but we can mantain this test as black box even without that.
Thanks 😁
PS. the PR #212 is already merged, so your pipeline should be pass this time
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I moved the test back into the errors
package and CI is green.
Consider making a release with these changes, it doesn't seem like there has been a release since 5th of January |
@johan-lejdung Yes, the idea is finish to check #213 (for conclude the support to the 1.13 errors) and see if another pending PR could be approved, and check the Milestone to v0.9.0 and then publish, we try this ASAP on this month, it is possible |
Would it make sense to also extend the The only reason I ask is because I've been able to include this package instead of the It's not the end of the world, but if this is thought of as a drop-and-replace package I think it would make sense. |
@johan-lejdung there a PR with the method |
* feat: support std errors functions add function `Is`, `As` and `Unwrap`, like std errors, so that we can continue to use pkg/errors with go1.13 compatibility Signed-off-by: Sherlock Holo <sherlockya@gmail.com> * style: delete useless comments Signed-off-by: Sherlock Holo <sherlockya@gmail.com> * build: update makefile update makefile to download dependencies before test anything Signed-off-by: Sherlock Holo <sherlockya@gmail.com> * build: fix makefile Signed-off-by: Sherlock Holo <sherlockya@gmail.com> * chore: delete useless comments Signed-off-by: Sherlock Holo <sherlockya@gmail.com> * Restore Makefile * revert: revert some change some change are doing by PR #206 and #212 , so I don't need to do it Signed-off-by: Sherlock Holo <sherlockya@gmail.com> * test: add more check for As unit test Signed-off-by: Sherlock Holo <sherlockya@gmail.com> * revert: only support Is As Unwrap for >=go1.13 Signed-off-by: Sherlock Holo <sherlockya@gmail.com> * feat(Unwrap): allow <go1.13 can use Unwrap `Unwrap` just use type assert, it doesn't need go1.13 actually Signed-off-by: Sherlock Holo <sherlockya@gmail.com> * test: add go1.13 errors compatibility check Signed-off-by: Sherlock Holo <sherlockya@gmail.com> * refactor(Unwrap): don't allow <go1.13 use Unwrap If we implement Unwrap ourselves, may create a risk of incompatibility if Go 1.14 subtly changes its `Unwrap` implementation. <go1.13 users doesn't have `Is` or `As`, if they want, they will use xerrors and it also provides `Unwrap` Signed-off-by: Sherlock Holo <sherlockya@gmail.com>
@aperezg Any chance we could get a tagged release for this? 🙏 Would be nice to no longer use a fork. |
Go 1.13 adds support for error chains to the standard libary's errors
package. The new standard library functions require an Unwrap method to
be provided by an error type. This change adds a new Unwrap method
(identical to the existing Cause method) to the unexported error types.