mirror of
https://github.com/pkg/errors.git
synced 2025-05-02 21:49:41 +00:00
Syntax coloring for Readme
I think it helps readability.
This commit is contained in:
parent
0d62637d04
commit
502e17a0cb
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user