-
Notifications
You must be signed in to change notification settings - Fork 91
make the package compatible with go error package #33
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
make the package compatible with go error package #33
Conversation
Signed-off-by: Viren Negi <63040+meetme2meat@users.noreply.github.com>
Interesting. I understand the problem (the interface value of error is not a nil interface because we're returning a typed nil from Wrap). This is a backward incompatible change. That said, I think it would improve the library (and if I were writing it today, it'd probably be like you suggest). On balance I'm going to merge this and add a section to the README that explains the breakage and how to work around it. |
Great Thanks. Let me know when you shell out a new release I'm waiting on it. |
it's out already as 1.3.0 |
@meetme2meat I ended up reverting this because it broke other people's workflows. Unfortunately that means that the only path forward to fixing this is either a v2, or a separate API that does what you want. |
yes, a v2 would be fine I can separate PR around it. |
I am currently use code like this (I saved the if code to a template): // g and h are functions in an external library not in my code.
func g() error {
...
}
func h() error {
...
}
func f() (err error) {
errRaw := g()
if errRaw != nil {
err = errors.Wrap(errRaw, 0)
return
}
errRaw = h()
if errRaw != nil {
err = errors.Wrap(errRaw, 0)
return
}
return
} This forwards only non nil errors. If error is nil I have no use for wrapping it. I have to check for nil anyway. You could also use |
I think it makes sense to include the behavior in this PR in a version 2. In some cases you do have to check for nil anyway but in other cases it would simplify code if one could write: func f() error {
// …
return errors.Wrap(g(), 0) // Always return != nil using latest go-errors
} Especially now that func WrapPrefix(e interface{}, prefix string, skip int) error {
if e == nil {
return nil
}
// …
} One thing that becomes slightly harder when Then you could write
Or it could be made to handle nil by returning an empty string or
Because of the go gotcha where nil is not nil it may be safest to use the second implementation. One may use the go-errors library by the book but other code may still return a typed nil somewhere which will make your code end up treating a success like and error which may propagate all the way to a central error handler where the stack trace is extracted. |
Fix: #32