Simple error handling primitives with stack traces and context for Go
npx @tessl/cli install tessl/golang-github-com--pkg--errors@0.9.0Package errors provides simple error handling primitives that extend Go's standard error interface with stack traces and context. It enables creating errors with automatic stack trace recording, wrapping existing errors with additional context while preserving the original error value, and retrieving the underlying cause of wrapped errors.
go get github.com/pkg/errorsimport "github.com/pkg/errors"Or with alias:
import pkgerrors "github.com/pkg/errors"package main
import (
"fmt"
"io/ioutil"
"github.com/pkg/errors"
)
func readConfig(path string) error {
data, err := ioutil.ReadFile(path)
if err != nil {
// Wrap adds context message and stack trace
return errors.Wrap(err, "failed to read config")
}
// Process data...
if len(data) == 0 {
// New creates error with stack trace
return errors.New("config file is empty")
}
return nil
}
func main() {
err := readConfig("/etc/app/config.yaml")
if err != nil {
// Print with stack trace using %+v
fmt.Printf("%+v\n", err)
// Get root cause
rootErr := errors.Cause(err)
fmt.Printf("Root cause: %v\n", rootErr)
}
}Create new errors with automatic stack trace recording.
// New returns an error with the supplied message.
// New also records the stack trace at the point it was called.
func New(message string) 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.
func Errorf(format string, args ...interface{}) errorUsage:
// Create simple error
err := errors.New("configuration not found")
// Create formatted error
err := errors.Errorf("invalid port: %d", port)Wrap existing errors with additional context and stack traces.
// Wrap returns an error annotating err with a stack trace
// at the point Wrap is called, and the supplied message.
// If err is nil, Wrap returns nil.
func Wrap(err error, message string) 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.
func Wrapf(err error, format string, args ...interface{}) errorUsage:
// Wrap with context message
file, err := os.Open(path)
if err != nil {
return errors.Wrap(err, "failed to open database")
}
// Wrap with formatted message
_, err = io.Copy(dest, src)
if err != nil {
return errors.Wrapf(err, "failed to copy %s to %s", src, dest)
}Add stack trace to an existing error without changing its message.
// WithStack annotates err with a stack trace at the point WithStack was called.
// If err is nil, WithStack returns nil.
func WithStack(err error) errorUsage:
// Add stack trace only
if err != nil {
return errors.WithStack(err)
}Add contextual message to an error without adding a stack trace.
// WithMessage annotates err with a new message.
// If err is nil, WithMessage returns nil.
func WithMessage(err error, message string) error
// WithMessagef annotates err with the format specifier.
// If err is nil, WithMessagef returns nil.
func WithMessagef(err error, format string, args ...interface{}) errorUsage:
// Add context without stack trace
if err != nil {
return errors.WithMessage(err, "database connection failed")
}
// Add formatted context
if err != nil {
return errors.WithMessagef(err, "retry %d/%d failed", attempt, maxRetries)
}Unwrap error chains to retrieve the root cause.
// Cause returns the underlying cause of the error, if possible.
// An error value has a cause if it implements the following interface:
//
// type causer interface {
// Cause() error
// }
//
// 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.
func Cause(err error) errorUsage:
// Get the root cause of wrapped errors
rootErr := errors.Cause(err)
// Type assert the root cause
switch errors.Cause(err).(type) {
case *os.PathError:
// handle file system errors
case *net.OpError:
// handle network errors
default:
// handle other errors
}Compatibility functions for Go 1.13+ error chains. These functions are only available when building with Go 1.13 or later.
// Is reports whether any error in err's chain matches target.
//
// The chain consists of err itself followed by the sequence of errors obtained by
// repeatedly calling Unwrap.
//
// An error is considered to match a target if it is equal to that target or if
// it implements a method Is(error) bool such that Is(target) returns true.
func Is(err, target error) bool
// As finds the first error in err's chain that matches target, and if so, sets
// target to that error value and returns true.
//
// The chain consists of err itself followed by the sequence of errors obtained by
// repeatedly calling Unwrap.
//
// An error matches target if the error's concrete value is assignable to the value
// pointed to by target, or if the error has a method As(interface{}) bool such that
// As(target) returns true. In the latter case, the As method is responsible for
// setting target.
//
// As will panic if target is not a non-nil pointer to either a type that implements
// error, or to any interface type. As returns false if err is nil.
func As(err error, target interface{}) bool
// Unwrap returns the result of calling the Unwrap method on err, if err's
// type contains an Unwrap method returning error.
// Otherwise, Unwrap returns nil.
func Unwrap(err error) errorUsage:
// Check if error matches a sentinel
if errors.Is(err, io.EOF) {
// handle EOF
}
// Extract typed error from chain
var pathErr *os.PathError
if errors.As(err, &pathErr) {
fmt.Printf("Failed to access: %s\n", pathErr.Path)
}
// Get immediate wrapped error
innerErr := errors.Unwrap(err)Extract and format stack traces from errors. Errors created by New, Errorf, Wrap, and Wrapf implement the stackTracer interface.
stackTracer Interface (not exported but part of stable public API):
type stackTracer interface {
StackTrace() StackTrace
}Usage:
// Type assert to extract stack trace
type stackTracer interface {
StackTrace() errors.StackTrace
}
if err, ok := err.(stackTracer); ok {
st := err.StackTrace()
fmt.Printf("%+v\n", st[0:2]) // Print top two frames
}All errors from this package implement fmt.Formatter with the following format verbs:
%s - print the error message. If the error has a Cause it will be printed recursively%v - same as %s%+v - extended format with full stack trace detailsUsage:
// Simple error message
fmt.Printf("%s\n", err) // "failed to read config: file not found"
// Extended format with stack traces
fmt.Printf("%+v\n", err) // Prints error with complete stack traceRepresents a program counter inside a stack frame.
type Frame uintptr// Format formats the frame according to the fmt.Formatter interface.
//
// %s source file
// %d source line
// %n function name
// %v equivalent to %s:%d
//
// Format accepts flags that alter the printing of some verbs, as follows:
//
// %+s function name and path of source file relative to the compile time
// GOPATH separated by \n\t (<funcname>\n\t<path>)
// %+v equivalent to %+s:%d
func (f Frame) Format(s fmt.State, verb rune)
// MarshalText formats a stacktrace Frame as a text string. The output is the
// same as that of fmt.Sprintf("%+v", f), but without newlines or tabs.
func (f Frame) MarshalText() ([]byte, error)Usage:
// Format individual frame
fmt.Printf("%+v\n", frame) // function name and full path with line
fmt.Printf("%s:%d\n", frame, frame) // file:line
fmt.Printf("%n\n", frame) // function name only
// Serialize frame to text
text, err := frame.MarshalText()Stack of Frames from innermost (newest) to outermost (oldest).
type StackTrace []Frame// Format formats the stack of Frames according to the fmt.Formatter interface.
//
// %s lists source files for each Frame in the stack
// %v lists the source file and line number for each Frame in the stack
//
// Format accepts flags that alter the printing of some verbs, as follows:
//
// %+v Prints filename, function, and line number for each Frame in the stack.
func (st StackTrace) Format(s fmt.State, verb rune)Usage:
// Format entire stack trace
fmt.Printf("%+v\n", stackTrace) // Full details for all frames
fmt.Printf("%v\n", stackTrace) // Compact format
fmt.Printf("%s\n", stackTrace) // Just filenamesThe causer interface is not exported but is part of the stable public API. Errors that wrap other errors implement this interface.
type causer interface {
Cause() error
}This interface is used internally by the Cause function to unwrap error chains. You can implement it in your own error types to make them compatible with this package.
Build contextual error chains by wrapping errors at each layer:
func validateConfig(data []byte) error {
if len(data) == 0 {
return errors.New("empty configuration")
}
return nil
}
func loadConfig(path string) error {
data, err := ioutil.ReadFile(path)
if err != nil {
return errors.Wrap(err, "failed to read file")
}
if err := validateConfig(data); err != nil {
return errors.Wrap(err, "invalid configuration")
}
return nil
}
func initApp() error {
if err := loadConfig("/etc/app.conf"); err != nil {
return errors.Wrap(err, "failed to initialize application")
}
return nil
}Wrap or Wrapf when you want to add both context and a new stack frameWithMessage or WithMessagef when you only want to add context without a new stack frameWithStack when you want to add only a stack frame without changing the message// Add context + stack trace (most common)
return errors.Wrap(err, "database query failed")
// Add only context (no new stack frame)
return errors.WithMessage(err, "validation failed")
// Add only stack trace (preserve original message)
return errors.WithStack(err)Always use %+v when logging errors during development to see full stack traces:
if err != nil {
// Development: see full stack trace
log.Printf("Error: %+v\n", err)
// Production: simpler output
log.Printf("Error: %v\n", err)
return err
}Use Cause to unwrap errors before type assertions:
switch err := errors.Cause(err).(type) {
case *json.SyntaxError:
log.Printf("JSON syntax error at byte %d", err.Offset)
case *os.PathError:
log.Printf("File operation failed: %s", err.Path)
default:
log.Printf("Unknown error: %v", err)
}This package is compatible with both traditional error handling and Go 1.13+ error chains:
// Traditional approach
rootErr := errors.Cause(err)
// Go 1.13+ approach (requires Go 1.13+)
if errors.Is(err, os.ErrNotExist) {
// handle not found
}
var pathErr *os.PathError
if errors.As(err, &pathErr) {
// work with pathErr
}WithStack when you need to preserve exact error values for comparisonCause before type asserting to get the original errorWrap for most cases, WithMessage when you don't need additional stack framesThis package has zero dependencies outside the Go standard library:
fmt - formattingio - I/O operationsruntime - stack trace capturepath - file path handlingstrconv - string conversionsstrings - string operationserrors (standard library) - Go 1.13+ error chain functions