-
Notifications
You must be signed in to change notification settings - Fork 128
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
error
nil check weirdness with Group.Wait
#57
Comments
By obtaining an |
See https://golang.org/doc/faq#nil_error still, i don’t get the need to return a pointer, just return a struct. |
@bfreis if err := g.Wait().(*multierror.Error)
errs := err.WrappedErrors()
... or var e *multierror.Error
if errors.As(g.Wait(), &e) {
// err is a *multierror.Error, and e is set to the error's value
errs := e.WrappedErrors()
...
} From that FAQ link above (thanks @stephanwesten), the Go maintainers recommend:
Seems like that would be a good pattern to follow here as well. It would certainly eliminate this sort of gotcha, which in my opinion, is quite an ugly wart of Go itself. |
Group.Wait
error
nil check weirdness with Group.Wait
It's just a bug in the library:
The signature should end with Cheers |
Changes the return value of Group.Wait to error instead of *Error. The *Error concrete type can lead to unintuitive, subtle bugs around nil checks (see: https://golang.org/doc/faq#nil_error). Returning the error interface instead eliminates these. Addresses hashicorp#57.
PR #58 addresses this issue by changing to |
Changes the return value of Group.Wait to error instead of *Error. The *Error concrete type can lead to unintuitive, subtle bugs around nil checks (see: https://golang.org/doc/faq#nil_error). Returning the error interface instead eliminates these. Addresses hashicorp#57.
I think an additional issue here is that e.g., Append(nil, nil) != nil It should really return return Append(&Error{}, newErrs...).ErrorOrNil() edit:
In this case, |
Not to sound like a total jerk, but I think it's customary to put a thanks @purpleidea or similar in the commit log when someone gives you the answer for your patch =D |
Seems this repository is dead anyway, considering even this tiny PR has sat nearly a year without as much as a glance. Also, not to be a total jerk @purpleidea, but did you really provide the answer? I mentioned the provided solution in the description of this issue when I opened it ;).
|
It's not uncommon to see code like this:
However, there is a subtle bug here. The
main
function will always panic, despite no error being returned by any of the Goroutines managed by the groupg
. This is due to the fact that Go treats interfaces as "fat pointers". See:A "fix" is to add the following nil check to the
Run
method above:This is certainly not intuitive, and relies on type-inference to solve the problem. I've added a test to my fork of this repo to clearly demonstrate the issue:
My question - is there a good reason why
Group.Wait
returns*Error
instead of justerror
, which is more idiomatic? Returningerror
would eliminate this weirdness seen here.The text was updated successfully, but these errors were encountered: