-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patherror.go
67 lines (54 loc) · 1.09 KB
/
error.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package avl
import (
"fmt"
"golang.org/x/xerrors"
)
// WrapError is simple wrapper for xerror.Is and xerror.As.
type WrapError struct {
S string
Err error
Frame xerrors.Frame
}
func NewWrapError(s string, a ...interface{}) WrapError {
return WrapError{S: fmt.Sprintf(s, a...)}
}
// Wrap put error inside WrapError.
func (we WrapError) Wrap(err error) error {
return WrapError{
S: we.S,
Err: err,
Frame: xerrors.Caller(1),
}
}
// Wrapf acts like `fmt.Errorf()`.
func (we WrapError) Wrapf(s string, a ...interface{}) error {
return WrapError{
S: we.S,
Err: xerrors.Errorf(s, a...),
Frame: xerrors.Caller(1),
}
}
// Is is for `xerrors.Is()`.
func (we WrapError) Is(err error) bool {
if err == nil {
return false
}
e, ok := err.(WrapError)
if !ok {
return false
}
return e.S == we.S
}
func (we WrapError) Unwrap() error {
return we.Err
}
func (we WrapError) FormatError(p xerrors.Printer) error {
we.Frame.Format(p)
return we.Unwrap()
}
func (we WrapError) Error() string {
if we.Err == nil {
return we.S
}
return fmt.Sprintf("%s; %v", we.S, we.Err)
}