-
Notifications
You must be signed in to change notification settings - Fork 4
/
errors.go
56 lines (47 loc) · 887 Bytes
/
errors.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
package cwrap
import (
"fmt"
"golang.org/x/xerrors"
)
type Error struct {
err error
msg string
frame xerrors.Frame
}
func Wrapf(err error, format string, v ...interface{}) error {
if err == nil {
return nil
}
return &Error{
err: err,
msg: fmt.Sprintf(format, v...),
frame: xerrors.Caller(1),
}
}
func Wrap(err error) error {
if err == nil {
return nil
}
return &Error{
err: err,
frame: xerrors.Caller(1),
}
}
func (e *Error) Error() string {
return e.err.Error()
}
// Unwrap implements xerrors.Wrapper
func (e *Error) Unwrap() error {
return e.err
}
// FormatError implements xerrors.Formatter
func (e *Error) FormatError(p xerrors.Printer) error {
p.Print(e.err)
p.Print(": ", e.msg)
e.frame.Format(p)
return nil
}
// Format implements fmt.Formatter
func (e *Error) Format(f fmt.State, verb rune) {
xerrors.FormatError(e, f, verb)
}