-
Notifications
You must be signed in to change notification settings - Fork 1
/
errors.go
167 lines (148 loc) · 2.83 KB
/
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
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package errors
import (
"fmt"
"time"
)
func New(message string) error {
return &errorUp{
msg: message,
cause: nil,
pcs: callers(),
occurred: timeNow(),
}
}
func Errorf(format string, args ...interface{}) error {
return &errorUp{
msg: fmt.Sprintf(format, args...),
cause: nil,
pcs: callers(),
occurred: timeNow(),
}
}
func NewWithDepth(depth int, skip int, message string) error {
return &errorUp{
msg: message,
cause: nil,
pcs: callersByAssigned(depth, skip),
occurred: timeNow(),
}
}
func ErrorWithDepthf(depth int, skip int, format string, args ...interface{}) error {
return &errorUp{
msg: fmt.Sprintf(format, args...),
cause: nil,
pcs: callersByAssigned(depth, skip),
occurred: timeNow(),
}
}
func With(cause error, message string) error {
if cause == nil {
return nil
}
return &errorUp{
msg: message,
cause: cause,
pcs: callers(),
occurred: timeNow(),
}
}
func Withf(cause error, format string, args ...interface{}) error {
if cause == nil {
return nil
}
return &errorUp{
msg: fmt.Sprintf(format, args...),
cause: cause,
pcs: callers(),
occurred: timeNow(),
}
}
func WithDepth(depth int, skip int, cause error, message string) error {
if cause == nil {
return nil
}
return &errorUp{
msg: message,
cause: cause,
pcs: callersByAssigned(depth, skip),
occurred: timeNow(),
}
}
func WithDepthf(depth int, skip int, cause error, format string, args ...interface{}) error {
if cause == nil {
return nil
}
return &errorUp{
msg: fmt.Sprintf(format, args...),
cause: cause,
pcs: callersByAssigned(depth, skip),
occurred: timeNow(),
}
}
func Wrap(e error) error {
return &errorUp{
msg: e.Error(),
cause: nil,
pcs: callers(),
occurred: timeNow(),
}
}
type errorUp struct {
msg string
cause error
pcs []uintptr
occurred time.Time
}
func (e *errorUp) Error() string {
return e.msg
}
func (e *errorUp) OccurTime() time.Time {
return e.occurred
}
func (e *errorUp) PCS() []uintptr {
return e.pcs
}
func (e *errorUp) Format(s fmt.State, verb rune) {
Configure().formatFn(s, verb, e)
}
func (e *errorUp) Cause() error {
if e.cause != nil {
return e.cause
}
return nil
}
func (e *errorUp) Contains(cause error) bool {
if e.cause == nil {
return false
}
err := e.cause
if err != nil {
if err == cause {
return true
}
hasCause, ok := err.(Errors)
if !ok {
return false
}
return hasCause.Contains(cause)
}
return false
}
func Contains(a error, b error) bool {
if a == nil || b == nil {
return false
}
e, ok := a.(Errors)
if !ok {
return false
}
return e.Contains(b)
}
type Errors interface {
Error() string
Cause() error
OccurTime() time.Time
PCS() []uintptr
Contains(error) bool
Format(fmt.State, rune)
}