-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery.go
231 lines (180 loc) · 6.23 KB
/
query.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package tower
import "errors"
// Query is a namespace group that holds the tower's Query functions.
//
// Methods and functions under Query are utilities to search values in the error stack.
const Query query = 0
type query uint8
/*
GetHTTPCode Search for any error in the stack that implements HTTPCodeHint and return that value.
The API searches from the outermost error, and will return the first value it found.
Return 500 if there's no error that implements HTTPCodeHint in the stack.
*/
func (query) GetHTTPCode(err error) (code int) {
if err == nil {
return 500
}
if ch, ok := err.(HTTPCodeHint); ok { //nolint:errorlint
return ch.HTTPCode()
}
return Query.GetHTTPCode(errors.Unwrap(err))
}
/*
GetCodeHint Search for any error in the stack that implements CodeHint and return that value.
The API searches from the outermost error, and will return the first value it found.
Return 500 if there's no error that implements CodeHint in the stack.
Used by Tower to search Code.
*/
func (query) GetCodeHint(err error) (code int) {
if err == nil {
return 500
}
if ch, ok := err.(CodeHint); ok { //nolint:errorlint
return ch.Code()
}
return Query.GetCodeHint(errors.Unwrap(err))
}
/*
GetMessage Search for any error in the stack that implements MessageHint and return that value.
The API searches from the outermost error, and will return the first value it found.
Return empty string if there's no error that implements MessageHint in the stack.
Used by Tower to search Message in the error.
*/
func (query) GetMessage(err error) (message string) {
if err == nil {
return ""
}
if ch, ok := err.(MessageHint); ok { //nolint:errorlint
return ch.Message()
}
return Query.GetMessage(errors.Unwrap(err))
}
/*
SearchCode Search the error stack for given code.
Given Code will be tested and the tower.Error is returned if:
1. Any of the error in the stack implements CodeHint interface, matches the given code, and can be cast to tower.Error.
2. Any of the error in the stack implements HTTPCodeHint interface, matches the given code, and can be cast to tower.Error.
Otherwise, this function will look deeper into the stack and
eventually returns nil when nothing in the stack implements those three and have the code.
The search operation is "Breath First", meaning the tower.Error is tested for CodeHint and HTTPCodeHint first before moving on.
*/
func (query) SearchCode(err error, code int) Error {
if err == nil {
return nil
}
// It's important to not use the other Search API for brevity.
// This is because we are
if ch, ok := err.(CodeHint); ok && ch.Code() == code { //nolint:errorlint
if err, ok := err.(Error); ok { //nolint:errorlint
return err
}
}
if ch, ok := err.(HTTPCodeHint); ok && ch.HTTPCode() == code { //nolint:errorlint
if err, ok := err.(Error); ok { //nolint:errorlint
return err
}
}
return Query.SearchCode(errors.Unwrap(err), code)
}
/*
SearchCodeHint Search the error stack for given code.
Given Code will be tested and the tower.Error is returned if any of the error in the stack implements CodeHint interface, matches the given code, and can be cast to tower.Error.
Otherwise, this function will look deeper into the stack and eventually returns nil when nothing in the stack implements CodeHint.
*/
func (query) SearchCodeHint(err error, code int) Error {
if err == nil {
return nil
}
if ch, ok := err.(CodeHint); ok && ch.Code() == code { //nolint:errorlint
if err, ok := err.(Error); ok { //nolint:errorlint
return err
}
}
return Query.SearchCodeHint(errors.Unwrap(err), code)
}
/*
SearchHTTPCode Search the error stack for given code.
Given Code will be tested and the tower.Error is returned if any of the error in the stack implements HTTPCodeHint interface, matches the given code, and can be cast to tower.Error.
Otherwise, this function will look deeper into the stack and eventually returns nil when nothing in the stack implements HTTPCodeHint.
*/
func (query) SearchHTTPCode(err error, code int) Error {
if err == nil {
return nil
}
if ch, ok := err.(HTTPCodeHint); ok && ch.HTTPCode() == code { //nolint:errorlint
if err, ok := err.(Error); ok { //nolint:errorlint
return err
}
}
return Query.SearchHTTPCode(errors.Unwrap(err), code)
}
// CollectErrors Collects all the tower.Error in the error stack.
//
// It is sorted from the top most error to the bottom most error.
func (query) CollectErrors(err error) []Error {
return collectErrors(err, nil)
}
func collectErrors(err error, input []Error) []Error {
if err == nil {
return input
}
if err, ok := err.(Error); ok { //nolint:errorlint
input = append(input, err)
}
return collectErrors(errors.Unwrap(err), input)
}
// GetStack Gets the error stack by checking CallerHint.
//
// Tower recursively checks the given error if it implements CallerHint until all the error in the stack are checked.
//
// If you wish to get list of tower.Error use CollectErrors instead.
func (query) GetStack(err error) []KeyValue[Caller, error] {
in := make([]KeyValue[Caller, error], 0, 10)
return getStackList(err, in)
}
func getStackList(err error, input []KeyValue[Caller, error]) []KeyValue[Caller, error] {
if err == nil {
return input
}
if ch, ok := err.(CallerHint); ok { //nolint:errorlint
return append(input, NewKeyValue(ch.Caller(), err))
}
return getStackList(errors.Unwrap(err), input)
}
// TopError Gets the outermost tower.Error instance in the error stack.
// Returns nil if no tower.Error instance found in the stack.
func (query) TopError(err error) Error {
if err == nil {
return nil
}
if e, ok := err.(Error); ok { //nolint:errorlint
return e
}
return Query.TopError(errors.Unwrap(err))
}
// BottomError Gets the outermost tower.Error instance in the error stack.
// Returns nil if no tower.Error instance found in the stack.
func (query) BottomError(err error) Error {
top := Query.TopError(err)
if top == nil {
return nil
}
var result Error
unwrapped := top.Unwrap()
for unwrapped != nil {
if e, ok := unwrapped.(Error); ok { //nolint:errorlint
result = e
}
unwrapped = errors.Unwrap(err)
}
return result
}
// Cause returns the root cause.
func (query) Cause(err error) error {
unwrapped := errors.Unwrap(err)
for unwrapped != nil {
err = unwrapped
unwrapped = errors.Unwrap(err)
}
return err
}