-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.go
81 lines (68 loc) · 1.52 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package zerrors
import (
"context"
"fmt"
)
// zError defines the error itself.
type zError struct {
msg string
errType fmt.Stringer
err error
frame Frame
ctx context.Context
}
// Error implements error interface.
func (e *zError) Error() string {
return fmt.Sprint(e)
}
// Unwrap implements Wrapper interface.
func (e *zError) Unwrap() error {
return e.err
}
// Format implements fmt.Formatter interface.
func (e *zError) Format(s fmt.State, v rune) { FormatError(e, s, v) }
// FormatError implements Formatter.
func (e *zError) FormatError(p Printer) (next error) {
p.Print(e.msg)
e.frame.Format(p)
return e.err
}
// Type implements TypeAwareError interface.
func (e *zError) Type() fmt.Stringer {
return e.errType
}
// IsType implements TypeAwareError interface.
func (e *zError) IsType(errType fmt.Stringer) bool {
return IsType(e, errType)
}
// Context implements ContextAwareError interface.
func (e *zError) Context() (ctx context.Context) {
return WalkErrChain(e, func(err error) interface{} {
_, ok := err.(*zError)
if !ok {
return nil
}
ctx = err.(*zError).ctx
if ctx != nil {
return ctx
}
return nil
}).(context.Context)
}
// ContextValue implements ContextAwareError interface.
func (e *zError) ContextValue(key interface{}) interface{} {
return WalkErrChain(e, func(err error) interface{} {
_, ok := err.(*zError)
if !ok {
return nil
}
ctx := err.(*zError).ctx
if ctx != nil {
val := ctx.Value(key)
if val != nil {
return val
}
}
return nil
})
}