|
1 | 1 | // Package errors implements functions for manipulating errors. |
| 2 | +// |
| 3 | +// The tranditional error handling idiom in Go is roughly akin to |
| 4 | +// |
| 5 | +// if err != nil { |
| 6 | +// return err |
| 7 | +// } |
| 8 | +// |
| 9 | +// which applied recursively up the call stack results in error reports |
| 10 | +// without context or debugging information. The errors package allows |
| 11 | +// programmers to add context to the failure path in their code in a way |
| 12 | +// that does not destroy the original value of the error. |
| 13 | +// |
| 14 | +// Adding context to an error |
| 15 | +// |
| 16 | +// The errors.Wrap function returns a new error that adds context to the |
| 17 | +// original error. For example |
| 18 | +// |
| 19 | +// _, err := ioutil.ReadAll(r) |
| 20 | +// if err != nil { |
| 21 | +// return errors.Wrap(err, "read failed") |
| 22 | +// |
| 23 | +// In addition, errors.Wrap records the file and line where it was called, |
| 24 | +// allowing the programmer to retrieve the path to the original error. |
| 25 | +// |
| 26 | +// Retrieving the cause of an error |
| 27 | +// |
| 28 | +// Using errors.Wrap constructs a stack of errors, adding context to the |
| 29 | +// preceeding error. Depending on the nature of the error it may be necessary |
| 30 | +// to recerse the operation of errors.Wrap to retrieve the original error |
| 31 | +// for inspection. Any error value which implements this interface |
| 32 | +// |
| 33 | +// type causer interface { |
| 34 | +// Cause() error |
| 35 | +// } |
| 36 | +// |
| 37 | +// Can be inspected by errors.Cause which will recursively retrieve the topmost |
| 38 | +// error which does nor implement causer, which is assumed to be the original |
| 39 | +// cause. For example: |
| 40 | +// |
| 41 | +// switch err := errors.Cause(err).(type) { |
| 42 | +// case *MyError: |
| 43 | +// // handle specifically |
| 44 | +// default: |
| 45 | +// // unknown error |
| 46 | +// } |
2 | 47 | package errors |
3 | 48 |
|
4 | 49 | import ( |
|
0 commit comments