-
Notifications
You must be signed in to change notification settings - Fork 18k
errors: unwrap joinError to avoid deeply-nested joins #65360
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
Conversation
This PR (HEAD: 2186af0) has been imported to Gerrit for code review. Please visit Gerrit at https://go-review.googlesource.com/c/go/+/559355. Important tips:
|
2186af0
to
1da0d1a
Compare
Message from Gopher Robot: Patch Set 1: (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/559355. |
This PR (HEAD: 1da0d1a) has been imported to Gerrit for code review. Please visit Gerrit at https://go-review.googlesource.com/c/go/+/559355. Important tips:
|
This PR is a quick proposal to unwrap `joinError` (and only `joinError`) when calling `errors.Join`. While I don't think we should unwrap everything, this is an internal structure that has only internally semantic meaning. Therefore, I think it makes sense to special-case `joinError` and unwrapped the contained errors on `joinError`. Pros: - More closely mirrors how other error joining packages in the ecosystem behave. - Even though `Unwrap() []error` isn't a public API, this will ensure that the method behaves as end-users expect for `errors.Join`, returning the list of joined errors. Cons: - Introduces type assertion and could result in a performance regression, since we're not pre-computing the slice length. This could be addressed if there's interest in pursuing this. I also think there's a reasonable argument to be made about the benefits realized from this change when printing the error. - Users could be depending on the broken behavior, but that seems unlikely since `joinError` is an internal API. I could not find any corresponding issues, but "errors" and "unwrap" are not particularly unique keywords, so apologies if I missed them.
1da0d1a
to
d43f5a6
Compare
Message from Seth Vargo: Patch Set 2: (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/559355. |
This PR (HEAD: d43f5a6) has been imported to Gerrit for code review. Please visit Gerrit at https://go-review.googlesource.com/c/go/+/559355. Important tips:
|
for _, err := range errs { | ||
if err != nil { | ||
n++ | ||
allErrs := make([]error, 0, len(errs)) |
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.
This might be one of those situations where optimizing for the common use case makes sense. Given "most" error joining is of the form merr = errors.Join(merr, err)
, we can optimize for the single-entry error:
switch len(errs) {
case 0:
return nil
case 1:
return errs[0]
case 2:
if errs[0] == nil {
return errs[1]
} else if errs[1] == nil {
return errs[0]
} else {
return &joinError{
errs: errs,
}
}
default:
// as below
}
Message from Ian Lance Taylor: Patch Set 3: (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/559355. |
Message from Seth Vargo: Patch Set 3: (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/559355. |
Message from Damien Neil: Patch Set 3: Hold+1 (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/559355. |
This PR is a quick proposal to unwrap
joinError
(and onlyjoinError
)when calling
errors.Join
. While I don't think we should unwrapeverything, this is an internal structure that has only internally
semantic meaning. Therefore, I think it makes sense to special-case
joinError
and unwrapped the contained errors onjoinError
.Pros:
More closely mirrors how other error joining packages in the ecosystem
behave.
Even though
Unwrap() []error
isn't a public API, this will ensurethat the method behaves as end-users expect for
errors.Join
,returning the list of joined errors.
Cons:
Introduces type assertion and could result in a performance
regression, since we're not pre-computing the slice length. This could
be addressed if there's interest in pursuing this. I also think there's
a reasonable argument to be made about the benefits realized from this
change when printing the error.
Users could be depending on the broken behavior, but that seems
unlikely since
joinError
is an internal API.I could not find any corresponding issues, but "errors" and "unwrap" are
not particularly unique keywords, so apologies if I missed them.