Skip to content

Commit

Permalink
Update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
roeldev committed Mar 1, 2023
1 parent 217a584 commit 2bd44b3
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,18 @@ import "github.com/go-pogo/errors"
Instead of defining error messages as global variables, it is possible to define
them as constants using `errors.Msg`.

const ErrSomethingWentWrong errors.Msg = "something went wrong"
```go
const ErrSomethingWentWrong errors.Msg = "something went wrong"
```

This same can be done with `errors.Kind`:

const InvalidOpError errors.Kind = "invalid operation error"
err = errors.WithKind(err, InvalidOpError)
```go
const InvalidOpError errors.Kind = "invalid operation error"
err = errors.WithKind(err, InvalidOpError)

errors.Is(err, InvalidOpError) // true
errors.Is(err, InvalidOpError) // true
```

## Formatting
Wrap an existing error with `errors.WithFormatter` to upgrade the error to
Expand All @@ -62,13 +66,17 @@ Formatting is done using `xerrors.FormatError` and thus the same verbs are
supported. Any error created with this package implements the `fmt.Formatter`
and `xerrors.Formatter` interfaces.

fmt.Printf("%+v", errors.WithFormatter(err))
```go
fmt.Printf("%+v", errors.WithFormatter(err))
```

## Stack tracing
Every error can track stack trace information. Just wrap it with
`errors.WithStack` and a complete stack trace is captured.

err = errors.WithStack(err)
```go
err = errors.WithStack(err)
```

An `errors.StackTrace` can be retrieved using `errors.GetStackTrace`.
Printing the error results in a trace similar to:
Expand All @@ -91,8 +99,10 @@ go build -tags=notrace
## Catching panics
A convenient function is available to catch panics and store them as an error.

var err error
defer errors.CatchPanic(&err)
```go
var err error
defer errors.CatchPanic(&err)
```

## Backwards compatibility
Unwrap, Is, As are backwards compatible with the standard library's `errors`
Expand Down

0 comments on commit 2bd44b3

Please sign in to comment.