Skip to content

Commit

Permalink
improve readme
Browse files Browse the repository at this point in the history
Signed-off-by: Tiago Peczenyj <tpeczenyj@weborama.com>
  • Loading branch information
peczenyj committed Nov 12, 2023
1 parent 56a34ad commit 3128936
Showing 1 changed file with 38 additions and 1 deletion.
39 changes: 38 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# errors
# errors

[![tag](https://img.shields.io/github/tag/peczenyj/errors.svg)](https://github.com/peczenyj/errors/releases)
![Go Version](https://img.shields.io/badge/Go-%3E%3D%201.18-%23007d9c)
Expand Down Expand Up @@ -43,6 +43,43 @@ Same for `WithMessage` and `WithMessagef` (however, here it is just an alias to

The useful function `Into` from `go-faster/errors` is also available.

## Features

| Feature | `errors` | `pkg/errors` | `go-faster/errors` | `peczenyj/errors` |
|-------------------------------------------------|----------|--------------|--------------------|-------------------|
| error constructors (`New`, `Errorf`) |||||
| error causes (`Cause` / `Unwrap`) | ||||
| type safety (`Into`) | | |||
| `errors.As()`, `errors.Is()` |||||
| support stack traces | ||| no, by desing |
|

## Motivation

If your project wants to minimize external dependencies and does not need stack traces on every error or failure, this is a acceptable alternative.

When migrating from some `github.com/<your favorite repository>/errors` to the standard lib `errors`, we can see that our code rely on non-standard functions such as `Wrap` or `Wrapf` that demands more changes than just update the import.

However, helper function such as `Wrap` or `Wrapf` are useful, since it highlight the **error** and automatically ignores nil values.

It means we can choose between:

```go
data, err := io.ReadAll(r)
if err != nil {
return errors.Wrap(err, "read failed") // add context
}
...
```

And a more simple approach:

```go
data, err := io.ReadAll(r)

return data, errors.Wrap(err, "read failed") // will return nil if err is nil
```

### werrors

You can keep using standard `errors` and import the `github.com/peczenyj/errors/werrors` to have access to some extra functions such as `Wrap`, `Wrapf`, `WithMessage`, `WithMessagef`, `Cause` and `Into`.
Expand Down

0 comments on commit 3128936

Please sign in to comment.