Skip to content
Drop-in replacement for the standard library errors package and github.com/pkg/errors
Go Makefile
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.circleci Reindent circle config Aug 8, 2019
.idea Initial commit Jul 9, 2019
tests Add integration tests Jul 12, 2019
utils/keyval
.editorconfig Initial commit Jul 9, 2019
.gitignore Initial commit Jul 9, 2019
.golangci.yml
CHANGELOG.md Update readme Aug 15, 2019
LICENSE
LICENSE_THIRD_PARTY Use uber multierr for now Jul 13, 2019
Makefile Update makefile Aug 8, 2019
README.md Update readme Aug 15, 2019
error_details.go Add details constructor Jul 16, 2019
error_details_test.go
errors.go Add constant error Aug 15, 2019
errors_multi.go
errors_new_test.go
errors_test.go Improve tests Jul 12, 2019
errors_with_message_test.go
errors_with_stack_test.go Add If functions Jul 12, 2019
errors_wrap_test.go
example_test.go
format_test.go Add with message error constructor Jul 9, 2019
go.mod Use uber multierr for now Jul 13, 2019
go.sum Use uber multierr for now Jul 13, 2019
main.mk Add integration tests Jul 12, 2019
stack.go Add stack implementation Jul 9, 2019
stack_test.go
wrap.go Add UnwrapEach function Jul 12, 2019
wrap_go1_12.go Add unwrapping logic Jul 9, 2019
wrap_go1_13.go Add unwrapping logic Jul 9, 2019
wrap_test.go Add UnwrapEach function Jul 12, 2019

README.md

Emperror: Errors

CircleCI Coverage Go Report Card GolangCI Go Version GoDoc

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
  • NewConstant is similar to NewPlain, but it creates a new 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

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.
var ErrSomethingWentWrong = errors.NewPlain("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
		}
	}
}

Development

When all coding and testing is done, please run the test suite:

$ make check

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.

You can’t perform that action at this time.