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.
go get github.com/rotisserie/eris
Check out thepackage docsfor more detailed information or connect with us on ourSlack channel
if you want to discuss anything in depth.**********************************How is eris different?
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-friendlyWrap
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 allowseris
to work with other error packages transparently and elimates the need to manage stack traces manually. Unlike other packages,
eris
also works well with global error types by automatically updating stack traces during error wrapping.
Types of errors
erisis 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.Newand. 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 via
eris.Errorfwill return the next error in the stack until a root error is reached.eris.Causeeris.Unwrap
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 theeris
package.
Wrapping errors with additional context
(eris.Wrapadds 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
Theeris package provides a few ways to inspect and compare error types.eris.Is
eris.Causereturns the root cause of the error. Currently,eris.Isworks 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 (ieeris.Is (will returntrue) ).
GIPHY App Key not set. Please check settings