Skip to content
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

Ensure wrapping nil is still nil #1

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

gabrielf
Copy link

This PR makes it possible to write code like:

func myCode() error {
    // …

    // Next line would previously always return a non nil error
    return errors.Wrap(libraryCode(), 0)
}

Accessing the stack trace becomes harder. Previously this could be done with errors.Wrap(err, 0).ErrorStack() but now that Wrap returns error you would have to write errors.Wrap(err, 0).(*errors.Error).ErrorStack(). To simplify access of the stack trace a new top level function has been added so that you can do: errors.ErrorStack(err) to access the stack trace.

If there is no stack trace

When `New`/`Wrap`/`WrapPrefix` returned `*Error` it was easy to access a stack trace with `errors.Wrap(err, 0).ErrorStack()` in cases where you knew that an error had occurred. Now that `New`/`Wrap`/`WrapPrefix` returns `error` you would have to write `errors.Wrap(err, 0).(*errors.Error).ErrorStack()` to get the same stack trace.

Using the convenience function this becomes simple again: `errors.ErrorStack(err)`. Note that this will panic if err is nil so should only be used when err is known to be not nil.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant