Pocket Gophers

Error Handling Packages

2018-02-20

I didn’t handle those errors because I don’t know how to.

On its surface error handling looks simple. Just call errors.New or fmt.Errorf, right?

How do you keep track of the context of an error deep in your call stack? How about when multiple errors happen at the same time, such as properly handling defer f.Close()?

Since errors are values, these more difficult situations can be handled with code. Here are some packages to help handle your errors.

github.com/Rolinh/errbag new

GoDoc Go project version Report card License BSD

Monitors the rate of errors and then takes a defined action when the error rate is too high. A higher-than-normal error rate may indicate some larger problem such as the network problems or a full disk.

github.com/SpiderOak/errstack new

GoDoc Go project version Report card License MIT

Use a stack to add context to errors as they are returned without losing the original error.

github.com/amattn/deeperror

GoDoc Go project version Report card License MIT

Add extensive context optimized for debugging to errors including:

All of this information is formatted when printed.

github.com/ansel1/merry

GoDoc Go project version Report card License MIT

Add context to errors:

Additionally, you can:

Based on and inspired by github.com/go-errors/errors, github.com/go-errgo/errgo, and github.com/amattn/deeperror.

github.com/aodin/errors new

GoDoc Go project version Report card License MIT

Create errors that marshal to JSON and XML with a Code ( int for e.g. HTTP status), Meta ( []string), and Fields ( map[string]string).

github.com/contiv/errored

GoDoc Go project version Report card License Apache 2.0

Combine multiple errors into a single error. Check if one of those errors is the error you are looking for (e.g., err == io.EOF, but for the entire collection).

Enable debug or trace information per error or globally. Debug adds the location the error was created, Trace adds the stacktrace.

github.com/go-errgo/errgo

GoDoc Go project version Report card License BSD

Provides ways to create, add context to, and inspect/diagnose errors. Combines many orthogonal features into a single package.

github.com/go-errors/errors

GoDoc Go project version Report card License MIT

Add a stacktrace to any error.

github.com/gogolfing/multierror new

GoDoc Go project version Report card License MIT

A slice of errors in a single error. Seems to do everything that is needed for this use case.

github.com/gravitational/trace new

GoDoc Go project version Report card License Apache 2.0

Preserves the original error and its origin while adding contextual information for error reports and logs.

github.com/hashicorp/errwrap new

GoDoc Go project version Report card License MPL2

Formalizes the pattern of wrapping errors, checking if a specific error is wrapped, and extracting wrapped errors.

github.com/hashicorp/go-multierror

GoDoc Go project version Report card License MPL 2

Represent a list of errors as a single error with a cutomizable format when the errors are printed.

github.com/jjeffery/errors

GoDoc Go project version Report card License MIT

Use a fluent interface to add context to errors:

github.com/juju/errors

GoDoc Go project version Report card License LGPL v3

Add messages as context to an existing error. Defines error sentinels and checks for common situations (e.g., method not allowed).

github.com/mpvl/errd

GoDoc Go project version Report card License BSD

Simplify error handling when dealing with defer.

github.com/ndrewnee/errs new

GoDoc Go project version Report card License MIT

Add context to an error message with a set of key-value pairs.

github.com/palantir/stacktrace

GoDoc Go project version Report card License Apache 2

Add context to errors along the call stack:

github.com/pkg/errors

GoDoc Go project version Report card License BSD

Add context to errors:

github.com/pnordahl/ergo new

GoDoc Go project version Report card License MIT

Use error templates to add identifiers, severity, categories, or other data to errors.

github.com/rhysd/locerr

GoDoc Go project version Report card License MIT

Build errors with pretty printing for your parsing/lexing/interpreting needs.

github.com/spacemonkeygo/errors

GoDoc Go project version Report card License Apache 2

Classify errors and determine if a specific error falls into that class using error hierarchies. Attach additional data to an error using arbitrary error values. Track where an error comes from using stack traces and track how an error was handled using exit recording. Collect errors together using ErrorGroup and its variants. Convert panics to errors with CatchPanic.

github.com/txgruppi/werr new

GoDoc Go project version Report card License MIT

Add file, line, and stack information to errors by wrapping them.

github.com/uber-go/multierr

GoDoc Go project version Report card License MIT

Combine one or more errors into a single error.

Dig into the Fundamentals of Go

Subscribe to receive a weekly email covering a Go fundamental. Be it the language, its tooling, or its packages, you will learn what you need to know.