Skip to content
This repository has been archived by the owner on Dec 1, 2021. It is now read-only.

feat: support std errors functions #213

Merged
merged 13 commits into from Jan 3, 2020

Conversation

Sherlock-Holo
Copy link
Contributor

add function Is, As and Unwrap, like std errors, so that we can
continue to use pkg/errors with go1.13 compatibility

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>
Signed-off-by: Sherlock Holo <sherlockya@gmail.com>
update makefile to download dependencies before test anything

Signed-off-by: Sherlock Holo <sherlockya@gmail.com>
Signed-off-by: Sherlock Holo <sherlockya@gmail.com>
Signed-off-by: Sherlock Holo <sherlockya@gmail.com>
.gitignore Outdated Show resolved Hide resolved
.travis.yml Outdated Show resolved Hide resolved
errors.go Outdated Show resolved Hide resolved
errors.go Outdated Show resolved Hide resolved
go.mod Outdated Show resolved Hide resolved
some change are doing by PR pkg#206 and pkg#212 , so I don't need to do it

Signed-off-by: Sherlock Holo <sherlockya@gmail.com>
Copy link

@puellanivis puellanivis left a comment

Choose a reason for hiding this comment

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

I think you’re building tests upon pure wrappers of external packages, which should not be our responsibility.

go113_test.go Show resolved Hide resolved
go113_test.go Outdated Show resolved Hide resolved
@jayschwa
Copy link
Contributor

If this package is going into maintenance mode (according to the README), why add new API surface area? "Maintenance mode" and "drop-in replacement" seem like competing motivations, especially as the standard library beefs up its package.

@abraithwaite
Copy link

abraithwaite commented Nov 11, 2019

If this package is going into maintenance mode (according to the README), why add new API surface area? "Maintenance mode" and "drop-in replacement" seem like competing motivations, especially as the standard library beefs up its package.

The change is small, it improves the user experience, and probably improves interoperability of tooling as well (less imports, don't have to alias the import). I think it would be silly not to include it personally.

@Sherlock-Holo
Copy link
Contributor Author

@aperezg please see are there anything need to improve, if has, let me know to optimize codes :-)

go113.go Outdated Show resolved Hide resolved
`Unwrap` just use type assert, it doesn't need go1.13 actually

Signed-off-by: Sherlock Holo <sherlockya@gmail.com>
Signed-off-by: Sherlock Holo <sherlockya@gmail.com>
errors.go Outdated
@@ -286,3 +286,13 @@ func Cause(err error) error {
}
return err
}

func Unwrap(err error) error {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why reimplement this function rather than wrapping it? Someone using Go 1.12 or older will not be using the new style of errors. This creates a risk of incompatibility if Go 1.14 subtly changes its Unwrap implementation.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@aperezg emm.... this is a problem if go1.14 changes Unwrap implment, so... we should keep Unwrap >= go1.13?

Copy link
Member

Choose a reason for hiding this comment

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

But why is the problem to maintain the Unwrap function like today? I understand that the Unwrap is an interface on a std library right? Sorry, I try to understand what is the point to change the implementation a wrapped with the standard library. I doubt that they implement a breaking change on that, no?

https://github.com/pkg/errors/blob/master/errors.go#L163

Choose a reason for hiding this comment

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

go1.14 cannot break the semantics of Unwrap without violating the go1 compat guarantee.

These same considerations apply to successive point releases. For instance, code that runs under Go 1.2 should be compatible with Go 1.2.1, Go 1.3, Go 1.4, etc., although not necessarily with Go 1.1 since it may use features added only in Go 1.2

Choose a reason for hiding this comment

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

This functionality should likely be folded into Cause() and Unwrap() here just calls Cause().

c.f. #215

Copy link
Contributor Author

Choose a reason for hiding this comment

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

as that says, 1.14 errors won't change implementation, so we could make unwrap continue in normal package @aperezg

Choose a reason for hiding this comment

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

That Unwrap only works for specific permutations of wrapping with the standard library and causers.

In the wild, we should expect any possible permutation of wrapping, including multiple times being wrapped in an alternating manner.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unwrap should only unwrap error one times, no matter the inner error implement unwrap interface or not, like std Unwrap do.

Choose a reason for hiding this comment

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

But this code would fail to fully unwrap: Unwrap(fmt.Errorf("wrap1: %w", myPrivateCauserOnlyError(fmt.Errorf("wrap 3: %w", err), "wrap2:"))) where I would get the fmt.Errorf("wrap3: %w", err) error not the original err, which would not be the expected behavior.

Choose a reason for hiding this comment

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

🤦‍♀ the standard library Unwrap does not continuously unwrap a function, until it reaches the end result, unlike how Cause in this package works.

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>
@mfridman
Copy link

@jayschwa why return nil instead of the original error that was passed in?

#213 (comment)

I.e., Unwrap may not get a causer or unwrapper, so shouldn't it return the error without modification?

@jayschwa
Copy link
Contributor

@jayschwa why return nil instead of the original error that was passed in?

Because we want to match the behavior of the standard library's Unwrap:

Unwrap returns the result of calling the Unwrap method on err, if err's type contains an Unwrap method returning error. Otherwise, Unwrap returns nil.

@johan-lejdung
Copy link

What's the status on this PR, it's been almost a month since the initiative to update this package started. I'd be happy to help out any way I can.

@Sherlock-Holo
Copy link
Contributor Author

Sherlock-Holo commented Nov 24, 2019 via email

@aperezg
Copy link
Member

aperezg commented Nov 24, 2019

Sorry I was very busy this month, I'll try to catch up with all comments ASAP

@Sherlock-Holo
Copy link
Contributor Author

@aperezg I think this PR should focus on add this functions for 1.13, and if many<1.13 users want them, we cloud create another PR to help them

@johan-lejdung
Copy link

I agree with that @Sherlock-Holo

The pr should focus on adding support for the new std errors functionalities. Rebuilding those functionalities for use on <1.13 doesn't make as much sense and isn't as pressing of an issue

@ajpetersons
Copy link

How soon could this get merged? I would really love to see this PR merged in master and tagged so we can use Go 1.13 functions.

@Sherlock-Holo
Copy link
Contributor Author

Sherlock-Holo commented Dec 6, 2019 via email

@aperezg
Copy link
Member

aperezg commented Dec 13, 2019

For me, I think that this PR would be only for add support to the new features on Go 1.13, so I think that as is now seems good.

these days I am a little busy too…maybe this weekend I can update this pr
for just support these functions >=1.13

@Sherlock-Holo What do yo need to update on this PR to be merged?

Furthermore I see that @puellanivis and @jayschwa I see that you also reviewed this PR, is good for you now?

@aperezg aperezg added this to the 0.9 milestone Dec 13, 2019
@Sherlock-Holo
Copy link
Contributor Author

@aperezg Nothing need to do before merge, now this PR only add new feature for >= 1.13

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants