Skip to content

Commit

Permalink
Switch aggregate to iterating directly within Is and As
Browse files Browse the repository at this point in the history
  • Loading branch information
strideynet committed Jun 27, 2022
1 parent a9f9a4c commit f3550c8
Showing 1 changed file with 16 additions and 25 deletions.
41 changes: 16 additions & 25 deletions trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -454,35 +454,26 @@ func (r aggregate) Error() string {
return output
}

// Unwrap implements `errors.Unwrap` in a somewhat unusual way to support
// `errors.Is` and `errors.As`.
//
// One might expect Unwrap to return the error at the head of the slice, but,
// instead we return the slice excluding the head. This, in combination with
// `aggregate.Is` re-invoking `errors.Is` on the head of the slice essentially
// allows for the original `errors.Is` to navigate the tree of error chains
// created by `aggregate`.
//
// Reading the implementation for `errors.Is` and `errors.As` should provide
// some further explanation.
func (r aggregate) Unwrap() error {
if len(r) == 1 {
return nil
}

return r[1:]
}

// Is implements the errors.Is interface by re-invoking errors.Is on the error
// at the head of the slice.
// Is implements the `Is` interface, by iterating through each error in the
// aggregate and invoking `errors.Is`.
func (r aggregate) Is(t error) bool {
return errors.Is(r[0], t)
for _, err := range r {
if errors.Is(err, t) {
return true
}
}
return false
}

// As implements the errors.As interface by re-invoking errors.As on the error
// at the head of the slice.
// As implements the `As` interface, by iterating through each error in the
// aggregate and invoking `errors.As`.
func (r aggregate) As(t interface{}) bool {
return errors.As(r[0], t)
for _, err := range r {
if errors.As(err, t) {
return true
}
}
return false
}

// Errors obtains the list of errors this aggregate combines
Expand Down

0 comments on commit f3550c8

Please sign in to comment.