-
Notifications
You must be signed in to change notification settings - Fork 0
/
cause_mask.go
81 lines (66 loc) · 1.67 KB
/
cause_mask.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package tracer
import (
"fmt"
"runtime"
)
func Cause(err error) error {
if err == nil {
return nil
}
e, ok := err.(*Error)
if ok {
if e.Wrpd != nil {
return e.Wrpd
}
}
return err
}
func Mask(err error) error {
if err == nil {
return nil
}
return mask(err)
}
func Maskf(e *Error, f string, v ...interface{}) error {
// The masking has to happen before annotating the error. Annotating the
// error before masking it would manipulate the error which we track as
// cause in Error.Wrpd.
err := mask(e)
// Only annotate the error once we masked it so that we do not manipulate
// the cause after we tracked it.
e = err.(*Error)
e.Anno = fmt.Sprintf(f, v...)
return e
}
func mask(err error) error {
// In case we get some arbitrary error, we create our own Error type so that
// we can properly work with it. The error we create ourselves gets simply
// annotated with the error message provided by the arbitrary error type.
e, ok := err.(*Error)
if !ok {
e = &Error{
Anno: err.Error(),
}
}
// In case we get our own Error type, we create a copy of it so that we do
// not manipulate the originally wrapped pointer during consecutive masking.
if ok {
e = e.Copy()
}
// If we got some arbitrary error or our known Error type was not wrapped
// yet, we want to wrap and fill it accordingly.
if !ok || (ok && e.Wrpd == nil) {
e.Type = fmt.Sprintf("%T", err)
e.Wrpd = err
}
// In all cases we want to fill the stack so that we can inspect the stack
// trace if we ever have to during debugging.
{
if e.Stck != "" {
e.Stck += ","
}
_, file, line, _ := runtime.Caller(2)
e.Stck += fmt.Sprintf("%s:%d", file, line)
}
return e
}