Package eris provides a better way to handle, trace, and log errors in Go. This package is inspired by a few existing packages:xerrors,
pkg / errors, andGo 1. 16 errors.
Check out thepackage docsfor more detailed information or connect with us on ourSlack channel
Named after the Greek goddess of strife and discord, this package is intended to give you more control over error handling via error wrapping, stack tracing, and output formatting. Basic error wrapping was added in Go 1. 13, but it omitted user-friendly
and built-in stack tracing. Other error packages provide some of the features found ineris
but without flexible control over error output formatting. This package provides default string and JSON formatters with options to control things like separators and stack trace output. However, it also provides an option to write custom formatters via
Error wrapping behaves somewhat differently than existing packages. It relies on root errors that contain a full stack trace and wrap errors that contain a single stack frame. When errors from other packages are wrapped, a root error is automatically created before wrapping it with the new context. This allows
to work with other error packages transparently and elimates the need to manage stack traces manually. Unlike other packages,
also works well with global error types by automatically updating stack traces during error wrapping.
Types of errors
is concerned with only three different types of errors: root errors, wrap errors, and external errors. Root and wrap errors are defined types in this package and all other error types are external or third-party errors.
Root errors are created via(eris.New
and. Generally, it's a good idea to maintain a set of root errors that are then wrapped with additional context whenever an error of that type occurs. Wrap errors represent a stack of errors that have been wrapped with additional context. Unwrapping these errors viaeris.Errorfwill return the next error in the stack until a root error is reached.eris.Cause
will also retrieve the root error.
When external error types are wrapped with additional context, a root error is first created from the original error. This creates a stack trace for the error and allows it to function with the rest of the
Wrapping errors with additional context
adds context to an error while preserving the type of the original error. This method behaves differently for each error type. For root errors, the stack trace is reset to the current callers which ensures traces are correct when using global / sentinel error values. Wrapped error types are simply wrapped with the new context. For external types (i.e. something other than root or wrap errors), a new root error is created for the original error and then it's wrapped with the additional context.
**************** (% v) ' **************************Inspecting error types returns true if a particular error appears anywhere in the error chain, and
returns the root cause of the error. Currently,
works simply by comparing error messages with each other. If an error contains a particular message anywhere in its chain (eg "not found"), it's defined to be that error type (ie
eris.Is (will returntrue) ).