Bez popisu

Dave Cheney c9e70be240 Makefile: switch to staticcheck (#187) před 7 roky
.gitignore 45e9319080 Initial commit před 10 roky
.travis.yml 537896ad6e travis: remove Go 1.8 and earlier (#182) před 7 roky
LICENSE a2d6902c6d LICENSE: remove trailing newline (#61) před 9 roky
Makefile c9e70be240 Makefile: switch to staticcheck (#187) před 7 roky
README.md 5ac96aea29 Update README.md před 7 roky
appveyor.yml f45f2b7903 add appveyor.yml (#36) před 9 roky
bench_test.go e1ac100e46 reduce allocations when printing stack traces (#149) před 8 roky
errors.go 937e8c5528 gofmt -w před 7 roky
errors_test.go e981d1a2c5 Add WithMessagef function (#118) před 7 roky
example_test.go 011399d349 Add WithStack and WithMessage tests před 9 roky
format_test.go f15c970de5 Remove an unused argument of utility test func (#139) před 8 roky
stack.go ee4766c291 Fix error during merge před 7 roky
stack_test.go 584cbace28 Remove checks for old style anon funcs (#186) před 7 roky

README.md

errors Travis-CI AppVeyor GoDoc Report card Sourcegraph

Package errors provides simple error handling primitives.

go get github.com/pkg/errors

The traditional error handling idiom in Go is roughly akin to

if err != nil {
        return err
}

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.

Adding context to an error

The errors.Wrap function returns a new error that adds context to the original error. For example

_, err := ioutil.ReadAll(r)
if err != nil {
        return errors.Wrap(err, "read failed")
}

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.

type causer interface {
        Cause() error
}

errors.Cause will recursively retrieve the topmost error which does not implement causer, which is assumed to be the original cause. For example:

switch err := errors.Cause(err).(type) {
case *MyError:
        // handle specifically
default:
        // unknown error
}

Read the package documentation for more information.

Roadmap

With the upcoming Go2 error proposals this package is moving into maintenance mode. The roadmap for a 1.0 release is as follows:

  • 0.9. Remove pre Go 1.9 support, address outstanding pull requests (if possible)
  • 1.0. Final release.

Contributing

Because of the Go2 errors changes, this package is not accepting proposals for new functionality. With that said, we welcome pull requests, bug fixes and issue reports.

Before sending a PR, please discuss your change by raising an issue.

License

BSD-2-Clause