[Awesome Go] Use errors to handle errors
errors
Package errors
provides simple error handling primitives.
The traditional error handling idiom in Go is roughly akin to
1 | if err != nil { |
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.
Installation
Standard go get:
1 | go get -u github.com/pkg/errors |
Usages
func Wrap - Adding context to an error
The errors.Wrap
function returns a new error that adds context to the original error. For example
1 | _, err := ioutil.ReadAll(r) |
Example:
1 | package main |
func Wrapf
1 | func Wrapf(err error, format string, args ...interface{}) error |
Wrapf
returns an error annotating err
with a stack trace at the point Wrapf
is called, and the format specifier. If err
is nil
, Wrapf
returns nil
.
1 | package main |
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 reverse 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
.
1 | type causer interface { |
errors.Cause
will recursively retrieve the topmost error which does not implement causer, which is assumed to be the original cause. For example:
1 | switch err := errors.Cause(err).(type) { |
Formatted printing of errors
1 | func (f Frame) Format(s fmt.State, verb rune) |
All error values returned from this package implement fmt.Formatter and can be formatted by the fmt package. The following verbs are supported:
1 | %s print the error. If the error has a Cause it will be |
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:
1 | type stackTracer interface { |
The returned errors.StackTrace
type is defined as
1 | type StackTrace []Frame |
The Frame
type represents a call site in the stack trace. Frame
supports the fmt.Formatter
interface that can be used for printing information about the stack trace of this error. For example:
1 | if err, ok := err.(stackTracer); ok { |
Although the stackTracer interface is not exported by this package, it is considered a part of its stable public interface.
1 | package main |
func Cause
1 | func Cause(err error) error |
Cause
returns the underlying cause of the error, if possible. An error value has a cause if it implements the following interface:
1 | type causer interface { |
If the error does not implement Cause
, the original error will be returned. If the error is nil, nil will be returned without further investigation.
Example:
1 | package main |
func Errorf
1 | func Errorf(format string, args ...interface{}) error |
Errorf
formats according to a format specifier and returns the string as a value that satisfies error. Errorf
also records the stack trace at the point it was called.
Example:
1 | package main |
func New
1 | func New(message string) error |
New
returns an error with the supplied message. New also records the stack trace at the point it was called.
Example:
1 | package main |
func WithMessage
1 | func WithMessage(err error, message string) error |
WithMessage
annotates err with a new message. If err
is nil
, WithMessage
returns nil
.
Example:
1 | package main |
func WithMessagef
1 | func WithMessagef(err error, format string, args ...interface{}) error |
WithMessagef
annotates err
with the format specifier. If err
is nil
, WithMessagef
returns nil
.
func WithStack
1 | func WithStack(err error) error |
WithStack
annotates err
with a stack trace at the point WithStack
was called. If err
is nil
, WithStack
returns nil
.
Example:
1 | package main |
Func Is, As, Unwrap
Func Is
, As
and Unwrap
is similar to Working with Errors in Go 1.13 - The Go Blog - https://blog.golang.org/go1.13-errors.
1 | func As(err error, target interface{}) bool |
References
[1] GitHub - pkg/errors: Simple error handling primitives - https://github.com/pkg/errors
[2] errors · pkg.go.dev - https://pkg.go.dev/github.com/pkg/errors
[3] errors - The Go Programming Language - https://golang.org/pkg/errors/
[4] Working with Errors in Go 1.13 - The Go Blog - https://blog.golang.org/go1.13-errors