/
errors.go
279 lines (250 loc) · 6.15 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
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package multiclient
import (
"strings"
"sync"
"github.com/hailo-platform/H2O/go-hailo-lib/multierror"
"github.com/hailo-platform/H2O/platform/client"
"github.com/hailo-platform/H2O/platform/errors"
)
type Errors interface {
error
// IgnoreUid removes all errors for the given request uid(s)
IgnoreUid(uids ...string) Errors
// IgnoreService removes all errors for requests to the given service(s)
IgnoreService(services ...string) Errors
// IgnoreEndpoint removes all errors to a given service endpoint
IgnoreEndpoint(service, endpoint string) Errors
// IgnoreType removes all errors of the given type(s)
IgnoreType(types ...string) Errors
// IgnoreCode removes all errors with the given code(s)
IgnoreCode(codes ...string) Errors
// Errors returns all matching errors, mapped to their request uid
Errors() map[string]errors.Error
// Count returns a count of matching errors
Count() int
// AnyErrors returns whether there are any contained errors
AnyErrors() bool
// ForUid returns the error for a given request uid (or nil)
ForUid(uid string) errors.Error
// Suffix sets a dotted suffix to add to any returned errors
Suffix(suffix string) Errors
// Combined returns a platform error with the given suffix, scoped correctly (or nil)
Combined() errors.Error
// MultiError returns a combined MultiError
MultiError() *multierror.MultiError
}
type reqErr struct {
*client.Request
err errors.Error
scoper Scoper
}
type errorsImpl struct {
sync.RWMutex
defaultScoper Scoper
errs map[string]reqErr // uid: reqErr
suffix string // applied to errs at the right time, but kept for Combined() errors
}
// copyLessCap returns a new *errorsImpl, with the same default scoper, and pre-prepared (but empty) requestScopes and
// errs maps.
// Note that this requires an existing read lock.
func (e *errorsImpl) copyLessCap(lessCap int) *errorsImpl {
newLen := len(e.errs) - lessCap
if newLen < 0 {
newLen = 0
}
return &errorsImpl{
defaultScoper: e.defaultScoper,
suffix: e.suffix,
errs: make(map[string]reqErr, newLen),
}
}
func (e *errorsImpl) set(uid string, req *client.Request, err errors.Error, scoper Scoper) *errorsImpl {
e.Lock()
defer e.Unlock()
if e.errs == nil {
e.errs = make(map[string]reqErr, 1)
}
e.errs[uid] = reqErr{
Request: req,
err: err,
scoper: scoper,
}
return e
}
func (e *errorsImpl) IgnoreUid(uids ...string) Errors {
if len(uids) == 0 {
return e
}
uidsMap := stringsMap(uids...)
e.RLock()
defer e.RUnlock()
result := e.copyLessCap(len(uids))
for uid, v := range e.errs {
if _, ok := uidsMap[uid]; !ok {
result.errs[uid] = v
}
}
return result
}
func (e *errorsImpl) IgnoreService(services ...string) Errors {
if len(services) == 0 {
return e
}
servicesMap := stringsMap(services...)
e.RLock()
defer e.RUnlock()
result := e.copyLessCap(len(services))
for uid, v := range e.errs {
if _, ok := servicesMap[v.Service()]; !ok {
result.errs[uid] = v
}
}
return result
}
func (e *errorsImpl) IgnoreEndpoint(service, endpoint string) Errors {
e.RLock()
defer e.RUnlock()
result := e.copyLessCap(1)
for uid, v := range e.errs {
if v.Service() != service && v.Endpoint() != endpoint {
result.errs[uid] = v
}
}
return result
}
func (e *errorsImpl) IgnoreType(types ...string) Errors {
if len(types) == 0 {
return e
}
typesMap := stringsMap(types...)
e.RLock()
defer e.RUnlock()
result := e.copyLessCap(len(types))
for uid, v := range e.errs {
if _, ok := typesMap[v.err.Type()]; !ok {
result.errs[uid] = v
}
}
return result
}
func (e *errorsImpl) IgnoreCode(codes ...string) Errors {
if len(codes) == 0 {
return e
}
codesMap := stringsMap(codes...)
e.RLock()
defer e.RUnlock()
result := e.copyLessCap(len(codes))
for uid, v := range e.errs {
if _, ok := codesMap[v.err.Code()]; !ok {
result.errs[uid] = v
}
}
return result
}
func (e *errorsImpl) Errors() map[string]errors.Error {
e.RLock()
defer e.RUnlock()
if len(e.errs) < 1 {
return nil
}
result := make(map[string]errors.Error, len(e.errs))
for uid, v := range e.errs {
result[uid] = v.err
}
return result
}
func (e *errorsImpl) Count() int {
e.RLock()
defer e.RUnlock()
return len(e.errs)
}
func (e *errorsImpl) AnyErrors() bool {
return e.Count() > 0
}
func (e *errorsImpl) ForUid(uid string) errors.Error {
e.RLock()
defer e.RUnlock()
if re, ok := e.errs[uid]; ok {
return re.err
}
return nil
}
func (e *errorsImpl) Suffix(suffix string) Errors {
suffix = strings.TrimPrefix(suffix, ".") // Remove trailing dot
if suffix == "" {
return e
}
e.RLock()
defer e.RUnlock()
result := e.copyLessCap(0)
if result.suffix != "" {
result.suffix += "."
}
result.suffix += suffix
for uid, re := range e.errs {
result.errs[uid] = reqErr{
Request: re.Request,
err: scopedErr{
errorType: re.err.Type(),
code: re.err.Code() + "." + suffix,
description: re.err.Description(),
context: re.err.Context(),
httpCode: re.err.HttpCode(),
multiStack: re.err.MultiStack(),
},
scoper: re.scoper}
}
return result
}
func (e *errorsImpl) Combined() errors.Error {
e.RLock()
defer e.RUnlock()
switch len(e.errs) {
case 0:
return nil
case 1:
for _, re := range e.errs {
return re.err
}
return nil
default:
// Figure out what Scoper to use for the error.
// If each request has the same From, use that, otherwise, use the defaultScoper
scoper, i := e.defaultScoper, 0
for _, re := range e.errs {
if re.scoper != nil && (i == 0 || scoper == nil || re.scoper == scoper) {
scoper = re.scoper
} else {
scoper = e.defaultScoper
}
i++
}
context := ""
if scoper != nil {
context = scoper.Context()
}
if e.suffix != "" {
if context != "" {
context += "."
}
context += e.suffix
}
return errors.InternalServerError(context, e.multiError().Error())
}
}
func (e *errorsImpl) multiError() *multierror.MultiError {
me := multierror.New()
for _, re := range e.errs {
me.Add(re.err)
}
return me
}
func (e *errorsImpl) MultiError() *multierror.MultiError {
e.RLock()
defer e.RUnlock()
return e.multiError()
}
func (e *errorsImpl) Error() string {
return e.MultiError().Error()
}