diff --git a/README.md b/README.md index a9f7d88..2cc5165 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ Package errors implements functions for manipulating errors. The traditional error handling idiom in Go is roughly akin to -``` +```go if err != nil { return err } @@ -13,7 +13,7 @@ which applied recursively up the call stack results in error reports without con ## Adding context to an error The errors.Wrap function returns a new error that adds context to the original error. For example -``` +```go _, err := ioutil.ReadAll(r) if err != nil { return errors.Wrap(err, "read failed") @@ -24,13 +24,13 @@ In addition, `errors.Wrap` records the file and line where it was called, allowi ## 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 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`. -``` +```go 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: -``` +```go switch err := errors.Cause(err).(type) { case *MyError: // handle specifically