or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/golang-github-com--pkg--errors

Simple error handling primitives with stack traces and context for Go

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
golangpkg:golang/github.com/pkg/errors@0.9.x

To install, run

npx @tessl/cli install tessl/golang-github-com--pkg--errors@0.9.0

index.mddocs/

github.com/pkg/errors

Package 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.

Package Information

  • Package Name: github.com/pkg/errors
  • Package Type: golang
  • Language: Go
  • Version: 0.9.1
  • Installation: go get github.com/pkg/errors

Core Imports

import "github.com/pkg/errors"

Or with alias:

import pkgerrors "github.com/pkg/errors"

Basic Usage

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)
	}
}

Capabilities

Error Creation

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{}) error

Usage:

// Create simple error
err := errors.New("configuration not found")

// Create formatted error
err := errors.Errorf("invalid port: %d", port)

Error Wrapping with Stack Trace

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{}) error

Usage:

// 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)
}

Stack Trace Annotation

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) error

Usage:

// Add stack trace only
if err != nil {
	return errors.WithStack(err)
}

Message Annotation

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{}) error

Usage:

// 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)
}

Error Chain Inspection

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) error

Usage:

// 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
}

Go 1.13+ Error Chain Functions

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) error

Usage:

// 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)

Stack Trace Extraction

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
}

Error Formatting

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 details

Usage:

// 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 trace

Types

Frame

Represents a program counter inside a stack frame.

type Frame uintptr

Frame Methods

// 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()

StackTrace

Stack of Frames from innermost (newest) to outermost (oldest).

type StackTrace []Frame

StackTrace Methods

// 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 filenames

Interfaces

causer Interface

The 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.

Common Patterns

Creating Error Chains

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
}

Choosing Between Wrap and WithMessage

  • Use Wrap or Wrapf when you want to add both context and a new stack frame
  • Use WithMessage or WithMessagef when you only want to add context without a new stack frame
  • Use WithStack 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)

Debugging with Stack Traces

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
}

Error Type Inspection

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)
}

Compatibility with Standard Library

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
}

Error Handling Best Practices

  1. Wrap errors at package boundaries: Add context when errors cross package boundaries
  2. Use %+v during development: Enable detailed stack traces during debugging
  3. Check for nil before wrapping: All wrapping functions return nil if the input error is nil
  4. Preserve sentinel errors: Use WithStack when you need to preserve exact error values for comparison
  5. Unwrap before type assertions: Always use Cause before type asserting to get the original error
  6. Choose appropriate wrapping: Use Wrap for most cases, WithMessage when you don't need additional stack frames

Dependencies

This package has zero dependencies outside the Go standard library:

  • fmt - formatting
  • io - I/O operations
  • runtime - stack trace capture
  • path - file path handling
  • strconv - string conversions
  • strings - string operations
  • errors (standard library) - Go 1.13+ error chain functions