Introduce Stacktrace and Frame (#37)

* Introduce Stacktrace and Frame

This PR is a continuation of a series aimed at exposing the stack trace
information embedded in each error value. The secondary effect is to
deprecated the `Fprintf` helper.

Taking cues from from @chrishines' `stack` package this PR introduces a
new interface `Stacktrace() []Frame` and a `Frame` type, similar in
function to the `runtime.Frame` type (although lacking its iterator
type). Each `Frame` implemnts `fmt.Formatter` allowing it to print
itself.

The older `Location` interface is still supported but also deprecated.
6 files changed
tree: a3d52ac9134ba22a116f10ef350c6f038b1ac629
  1. .gitignore
  2. .travis.yml
  3. appveyor.yml
  4. errors.go
  5. errors_test.go
  6. example_test.go
  7. LICENSE
  8. README.md
  9. stack.go
  10. stack_test.go
README.md

errors Travis-CI AppVeyor GoDoc Report card

Package errors provides simple error handling primitives.

The traditional error handling idiom in Go is roughly akin to

if err != nil {
        return err
}

which applied recursively up the call stack results in error reports without context or debugging information. The errors package allows programmers to add context to the failure path in their code in a way that does not destroy the original value of the error.

Adding context to an error

The errors.Wrap function returns a new error that adds context to the original error. For example

_, err := ioutil.ReadAll(r)
if err != nil {
        return errors.Wrap(err, "read failed")
}

Retrieving the stack trace of an error or wrapper

New, Errorf, Wrap, and Wrapf record a stack trace at the point they are invoked. This information can be retrieved with the following interface.

type Stacktrace interface {
        Stacktrace() []Frame
}

The Frame type represents a call site in the stacktrace. Frame supports the fmt.Formatter interface that can be used for printing information about the stacktrace of this error. For example

if err, ok := err.(Stacktrace); ok {
        fmt.Printf("%+s:%d", err.Stacktrace())
}

See the documentation for Frame.Format for more details.

Retrieving the cause of an error

Using errors.Wrap constructs a stack of errors, adding context to the preceding error. Depending on the nature of the error it may be necessary to recurse the operation of errors.Wrap to retrieve the original error for inspection. Any error value which implements this interface can be inspected by errors.Cause.

type causer interface {
        Cause() error
}

errors.Cause will recursively retrieve the topmost error which does not implement causer, which is assumed to be the original cause. For example:

switch err := errors.Cause(err).(type) {
case *MyError:
        // handle specifically
default:
        // unknown error
}

Would you like to know more? Read the blog post.

Contributing

We welcome pull requests, bug fixes and issue reports. With that said, the bar for adding new symbols to this package is intentionally set high.

Before proposing a change, please discuss your change by raising an issue.

Licence

MIT