Skip to content

Commit 3a6c47d

Browse files
committed
Update readme
1 parent ad28acf commit 3a6c47d

1 file changed

Lines changed: 8 additions & 3 deletions

File tree

‎README.md‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,14 +137,18 @@ return errors.New("could not process batch").WithCause(errs)
137137

138138
Wrapping an `*Error`, with `Wrap` or with `Newf` and `%w`, reuses its stack trace, which is closer to where the error
139139
was raised. Only the `Unwrap() error` chain is searched: a collection, such as `Join`, gets the current stack trace.
140-
The fields and cause of the wrapped error stay on it, reachable with `errors.As`:
140+
The fields and cause of the wrapped error stay on it. `errors.As` on the wrapper finds the wrapper itself, so the
141+
wrapped error is reached through the underlying error, or by a walk of `Unwrap() []error` collecting the fields of
142+
every `*Error` in the tree:
141143

142144
```go
143145
outer := errors.Newf("load user: %w", err)
144146

145147
outer.StackTrace() // the stack trace of err
146148
outer.Fields() // map[]
147149
errors.Is(outer, ErrNotFound) // true
150+
151+
inner, _ := errors.AsType[*errors.Error](outer.Unwrap()[0]) // err, with its fields
148152
```
149153

150154
### Collecting
@@ -209,8 +213,9 @@ fmt.Printf("%+v", err)
209213
```
210214

211215
`%+v` applies to the underlying error and to every cause, so their details are printed too. An `*Error` wrapped with
212-
`%w` is the exception: `fmt` prints only the wrapper's message, so its fields and cause are left out, reachable with
213-
`errors.As`. A `MultiError` joins its members with newlines, as `errors.Join` does, with `%+v` applied to every member.
216+
`%w` is the exception: `fmt` prints only the wrapper's message, so its fields and cause are left out; they stay on
217+
it, reached as under Wrapping. A `MultiError` joins its members with newlines, as `errors.Join` does, with `%+v`
218+
applied to every member.
214219
Wrapping a `MultiError` in an `*Error` prints its members first, so the fields and stack trace of the `*Error` follow
215220
the last member at the same indentation.
216221

0 commit comments

Comments
 (0)