Skip to content

Commit

Permalink
Add attach and mask functions for joining errors in a different way
Browse files Browse the repository at this point in the history
The `Attach` and `Mask` functions create a new error node that returns
the error message from one node, but creates a structure where the
additional errors are in the error chain and can be discovered with
`errors.Is` and `errors.As`.

Signed-off-by: Jonathan A. Sternberg <[email protected]>
  • Loading branch information
jsternberg committed Jul 22, 2024
1 parent 124d0dc commit e438d5a
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,3 +409,37 @@ func (c customMessage) As(target any) bool {
func (c customMessage) Error() string {
return c.msg
}

// Attach will join the extra errors to the base error where
// only the base error is part of the message but all errors
// are wrapped at the same level.
func Attach(base error, extra ...error) error {
return &attachedError{
error: base,
wrapped: append([]error{base}, extra...),
}
}

// Mask will join the extra errors where the base error is
// part of the message but the extra errors are joined earlier
// in the chain and would mask the base error when used with
// errors.Is or errors.As.
func Mask(base error, extra ...error) error {
return &attachedError{
error: base,
wrapped: append(extra, base),
}
}

// attachedError is used to attach errors to a base
// error. The attached errors will be hidden from view when
// being printed but will show up when errors.Is or errors.As
// are used.
type attachedError struct {
error
wrapped []error
}

func (e *attachedError) Unwrap() []error {
return e.wrapped
}

0 comments on commit e438d5a

Please sign in to comment.