-
Notifications
You must be signed in to change notification settings - Fork 39
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
Consider not Propagating nil #8
Comments
+1 -- I don't like the behavior of passing a |
Weak disagreement. I would note that the superior errors package supports the return As icing on top, we can ditch our stacktrace parser that extracts the cause messages (see deployctl's nostacktrace). |
OK, after thinking about this a bit more, I realize that my initial language was not precise enough/didn't fully capture my intent. I actually do agree that Instead, my contention is that making use of this behavior as client code is bad style because it obfuscates intention/behavior -- someone needs to understand the API/look under the covers to see what it's doing, and it's confusing because you see the error message right next to the call, so you have a tendency to think that an error is occurring. To me, this is unambiguous: if err := doSomething(); err != nil {
return {propagate|wrap}(err, "message")
}
return nil And while the following is more compact, it takes more cognitive overhead for me to understand that, if the function doesn't error, the rest of the call is basically a no-op: return {propagate|wrap}(doSomething(), "message") It may just be a matter of getting used to it, but that's my view. |
On the macro point, I'm definitely interested in trying out |
RE: what would it take:
|
I still disagree with your argument that the propagate/wrap call with possibly nil error is bad style. Let's observe how error handling looks like in idiomatic go:
This is common throughout the standard library, and I'd argue the most sensible way to attach context to this error is:
I think it makes it unnecessarily verbose to handle the
Isn't the obsfucation correct here? |
Caught up with Nick offline. I'm warming up to the idea of only wrapping non-nil errors. The example that convinced me is |
I absolutely love this library and use it in all our Go code, but Propagate allowing
EDIT: An alternative idea would be making a |
For anyone in the future: we hit yet another bug where We therefore forked this repo and made Propagate panic when its cause is |
Its not a bug when its the programmer's own mistake for misusing the function..... Adding a panic comes with huge risk of breaking your app, if anything you should use static code analysis like linter instead to prevent the misuse. If you add panic it will not be detected anyway before the code is comitted, you would have to wait until it is deployed to production -> wait until panic occured -> then fix it, which is very costly imo |
I realize it is probably too late to change this behavior, but I still think it is worthwhile flagging a bug I found recently. if you have already declared err in your scope (happens frequently), the following code can return nil on an error condition
the loss is not being able to do compact single-line returns, which is arguably poor go style anyways
The text was updated successfully, but these errors were encountered: