-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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
errors: Using errors.Join
on a joinError
should expand the existing joined error
#60209
Comments
We explicitly decided against this in #53435 (comment) |
@seankhliao thanks for the link, although I'm a bit bummed the code above (which looks cleaner than the alternative of collecting all errors in a slice first) can't be used :( Would it be possible to have this included in the docs so it's clear the example above is not the intended use? |
@ItalyPaleAle I'm not clear on how the docs should be changed. Do you want to send a patch or at least outline the doc change here? Thanks. |
Updates the documentation for Join to explain that it does not append to an existing joinError (i.e. the result of calling Join), and if one of the errors is a joinError, it will be wrapped again. See golang#60209 It also adds a test to provide an example of said behavior, as well as formalizing it in the API's contract.
@ianlancetaylor I have opened #60215. It's my first contribution to Go itself so I hope I did it right :) |
Change https://go.dev/cl/495076 mentions this issue: |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
Description
Consider this code (Playground)
This happens because when calling
errors.Join
, it does not check if one of the errors is ajoinError
already, so it creates a new one every time.err = errors.Join(err, errors.New("err1"))
,err
is ajoinError
with 1 item inside (and that's expected)err = errors.Join(err, errors.New("err2"))
,err
is ajoinError
in which item 1 is ajoinError
itself, and item 2 is the error.err = errors.Join(err, nil)
,err
is ajoinError
that contains 2joinError
's (and the first one contains ajoinError
itself)We found this bug while trying to implement a pattern where we collect all errors from a channel. We have N goroutines running in parallel, and we wanted to use this pattern to both collect errors (which could be nil) and wait for all goroutines to complete:
In the case above, calling
Unwrap() []error
on the result lead to unexpected results because of errors being wrapped multiple times injoinError
objectsThe text was updated successfully, but these errors were encountered: