-
Notifications
You must be signed in to change notification settings - Fork 0
/
wraperror.go
42 lines (36 loc) · 1.23 KB
/
wraperror.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package wraperrors
import "errors"
type wrapError struct {
wrapper error
wrapped error
}
// New creates a new wrap error
func New(wrapper, wrapped error) error {
return &wrapError{
wrapper: wrapper,
wrapped: wrapped,
}
}
// Error returns a string describing the wrap error and allows warp errors to implement the error interface
func (err *wrapError) Error() string {
return err.wrapper.Error() + ", " + err.wrapped.Error()
}
// Is compares the equality of a warp error to a target error. This method allows wrap errors to implement ths errors Is
// interface, and allows wrap errors to be compared using `errors.Is(myWrapError, anyOtherError)`
//
// A wrap error is equal to a target error if:
//
// (a) the wrap error is exactly equal (== operator) to the target error,
//
// (b) the wrapper error is equal to the target error using the errors.Is function, or
//
// (c) the wrapped error is equal to the target error using the errors.Is function
//
// (notice that b and c differ on the word wrapper/wrapped)
func (err *wrapError) Is(target error) bool {
return err == target || errors.Is(err.wrapper, target) || errors.Is(err.wrapped, target)
}
// Unwrap returns the wrapped error
func (err *wrapError) Unwrap() error {
return err.wrapped
}