commit | 01fff4b0662c197252e11b1e3a7cefc84b0d022c | [log] [tgz] |
---|---|---|
author | Dave Cheney <dave@cheney.net> | Tue May 10 11:02:00 2016 +1000 |
committer | Dave Cheney <dave@cheney.net> | Tue May 10 11:02:00 2016 +1000 |
tree | 12e818305cdb69b90773a88ff634bc4a6a36a397 | |
parent | 502e17a0cb34bd200593552492e458d89cf6a64f [diff] |
Remove errors.Print (#23) errors.Print had a number of problems. Firstly, it was hard coded to print to os.Stderr, which made it difficult to test, and hard to write an example test for. Secondly, comments made in issue #20 make it clear that helpers need to have a high bar for inclusion in this package, and something that wrapped errors.Fprint in a way that was hard to test fails that bar. So, Remove errors.Print, which frees the identifier for being reused later, and reduces the size of the package.
Package errors implements functions for manipulating 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.
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")
}
In addition, errors.Wrap
records the file and line where it was called, allowing the programmer to retrieve the path to the original 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 recurse 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 }
Would you like to know more? Read the blog post.
We welcome pull requests, bug fixes and issue reports. With that said, the bar for adding new symbols to this package is intentionally set high.
Before proposing a change, please discuss your change by raising an issue.
MIT