Categoryemperror.dev/errors
modulepackage
0.8.1
Repository: https://github.com/emperror/errors.git
Documentation: pkg.go.dev

# README

Emperror: Errors Mentioned in Awesome Go

GitHub Workflow Status Codecov Go Report Card Go Version PkgGoDev FOSSA Status

Drop-in replacement for the standard library errors package and github.com/pkg/errors.

This is a single, lightweight library merging the features of standard library errors package and github.com/pkg/errors. It also backports a few features (like Go 1.13 error handling related features).

Standard library features:

  • New creates an error with stack trace
  • Unwrap supports both Go 1.13 wrapper (interface { Unwrap() error }) and pkg/errors causer (interface { Cause() error }) interface
  • Backported Is and As functions

github.com/pkg/errors features:

  • New, Errorf, WithMessage, WithMessagef, WithStack, Wrap, Wrapf functions behave the same way as in the original library
  • Cause supports both Go 1.13 wrapper (interface { Unwrap() error }) and pkg/errors causer (interface { Cause() error }) interface

Additional features:

  • NewPlain creates a new error without any attached context, like stack trace
  • Sentinel is a shorthand type for creating constant error
  • WithStackDepth allows attaching stack trace with a custom caller depth
  • WithStackDepthIf, WithStackIf, WrapIf, WrapIff only annotate errors with a stack trace if there isn't one already in the error chain
  • Multi error aggregating multiple errors into a single value
  • NewWithDetails, WithDetails and Wrap*WithDetails functions to add key-value pairs to an error
  • Match errors using the match package

Installation

go get emperror.dev/errors

Usage

package main

import "emperror.dev/errors"

// ErrSomethingWentWrong is a sentinel error which can be useful within a single API layer.
const ErrSomethingWentWrong = errors.Sentinel("something went wrong")

// ErrMyError is an error that can be returned from a public API.
type ErrMyError struct {
	Msg string
}

func (e ErrMyError) Error() string {
	return e.Msg
}

func foo() error {
	// Attach stack trace to the sentinel error.
	return errors.WithStack(ErrSomethingWentWrong)
}

func bar() error {
	return errors.Wrap(ErrMyError{"something went wrong"}, "error")
}

func main() {
	if err := foo(); err != nil {
		if errors.Cause(err) == ErrSomethingWentWrong { // or errors.Is(ErrSomethingWentWrong)
			// handle error
		}
	}

	if err := bar(); err != nil {
		if errors.As(err, &ErrMyError{}) {
			// handle error
		}
	}
}

Match errors:

package main

import (
    "emperror.dev/errors"
    "emperror.dev/errors/match"
)

// ErrSomethingWentWrong is a sentinel error which can be useful within a single API layer.
const ErrSomethingWentWrong = errors.Sentinel("something went wrong")

type clientError interface{
    ClientError() bool
}

func foo() error {
	// Attach stack trace to the sentinel error.
	return errors.WithStack(ErrSomethingWentWrong)
}

func main() {
    var ce clientError
    matcher := match.Any{match.As(&ce), match.Is(ErrSomethingWentWrong)}

	if err := foo(); err != nil {
		if matcher.MatchError(err) {
			// you can use matchers to write complex conditions for handling (or not) an error
            // used in emperror
		}
	}
}

Development

Contributions are welcome! :)

  1. Clone the repository
  2. Make changes on a new branch
  3. Run the test suite:
    ./pleasew build
    ./pleasew test
    ./pleasew gotest
    ./pleasew lint
    
  4. Commit, push and open a PR

License

The MIT License (MIT). Please see License File for more information.

Certain parts of this library are inspired by (or entirely copied from) various third party libraries. Their licenses can be found in the Third Party License File.

FOSSA Status

# Packages

No description provided by the author
No description provided by the author
No description provided by the author

# Functions

Append appends the given errors together.
As finds the first error in err's chain that matches the type to which target points, and if so, sets the target to its value and returns true.
Cause returns the last error (root cause) in an err's chain.
Combine combines the passed errors into a single error.
Errorf returns a new error with a formatted message and annotated with stack trace at the point Errorf is called.
GetDetails extracts the key-value pairs from err's chain.
GetErrors returns a slice containing zero or more errors that the supplied error is composed of.
Is reports whether any error in err's chain matches target.
New returns a new error annotated with stack trace at the point New is called.
NewPlain returns a simple error without any annotated context, like stack trace.
NewWithDetails returns a new error annotated with stack trace at the point NewWithDetails is called, and the supplied details.
Unwrap returns the result of calling the Unwrap method on err, if err implements Unwrap.
UnwrapEach loops through an error chain and calls a function for each of them.
WithDetails annotates err with with arbitrary key-value pairs.
WithMessage annotates err with a new message.
WithMessagef annotates err with the format specifier.
WithStack annotates err with a stack trace at the point WithStack was called.
WithStackDepth annotates err with a stack trace at the given call depth.
WithStackDepthIf behaves the same way as WithStackDepth except it does not annotate the error with a stack trace if there is already one in err's chain.
WithStackIf behaves the same way as WithStack except it does not annotate the error with a stack trace if there is already one in err's chain.
Wrap returns an error annotating err with a stack trace at the point Wrap is called, and the supplied message.
Wrapf returns an error annotating err with a stack trace at the point Wrapf is called, and the format specifier.
WrapIf behaves the same way as Wrap except it does not annotate the error with a stack trace if there is already one in err's chain.
WrapIff behaves the same way as Wrapf except it does not annotate the error with a stack trace if there is already one in err's chain.
WrapIfWithDetails returns an error annotating err with a stack trace at the point WrapIfWithDetails is called, and the supplied message and details.
WrapWithDetails returns an error annotating err with a stack trace at the point WrapWithDetails is called, and the supplied message and details.

# Type aliases

Frame represents a program counter inside a stack frame.
Sentinel is a simple error without any annotated context, like stack trace.
StackTrace is stack of Frames from innermost (newest) to outermost (oldest).