-
Notifications
You must be signed in to change notification settings - Fork 0
/
wrap.go
63 lines (52 loc) · 858 Bytes
/
wrap.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
package errorx
import "fmt"
func Trace(err error) error {
if err == nil {
return nil
}
frame := Frame{}
if Tracing() {
frame = caller(1)
}
return &wrapError{
err: err,
frame: frame,
}
}
func Wrapf(err error, format string, a ...any) error {
if err == nil {
return nil
}
frame := Frame{}
if Tracing() {
frame = caller(1)
}
msg := format
if a != nil {
msg = fmt.Sprintf(format, a...)
}
return &wrapError{
err: err,
msg: msg,
frame: frame,
}
}
type wrapError struct {
err error
msg string
frame Frame
}
func (e *wrapError) Error() string {
return fmt.Sprint(e)
}
func (e *wrapError) Format(s fmt.State, v rune) {
FormatError(e, s, v)
}
func (e *wrapError) FormatError(p Printer) (next error) {
p.Print(e.msg)
e.frame.Format(p)
return e.err
}
func (e *wrapError) Unwrap() error {
return e.err
}