-
Notifications
You must be signed in to change notification settings - Fork 16
/
errors.go
499 lines (403 loc) · 13.7 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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
/*
* Atree - Scalable Arrays and Ordered Maps
*
* Copyright Flow Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package atree
import (
"errors"
"fmt"
"runtime/debug"
)
type ExternalError struct {
msg string
err error
}
func NewExternalError(err error, msg string) error {
return &ExternalError{msg: msg, err: err}
}
func (e *ExternalError) Error() string {
if e.msg == "" {
return e.err.Error()
}
return fmt.Sprintf("%s: %s", e.msg, e.err.Error())
}
func (e *ExternalError) Unwrap() error {
return e.err
}
type UserError struct {
err error
}
func NewUserError(err error) error {
return &UserError{err: err}
}
func (e *UserError) Error() string {
return e.err.Error()
}
func (e *UserError) Unwrap() error {
return e.err
}
type FatalError struct {
err error
}
func NewFatalError(err error) error {
return &FatalError{err: err}
}
func (e *FatalError) Error() string {
return e.err.Error()
}
func (e *FatalError) Unwrap() error {
return e.err
}
// SliceOutOfBoundsError is returned when index for array slice is out of bounds.
type SliceOutOfBoundsError struct {
startIndex uint64
endIndex uint64
min uint64
max uint64
}
// NewSliceOutOfBoundsError constructs a SliceOutOfBoundsError.
func NewSliceOutOfBoundsError(startIndex, endIndex, min, max uint64) error {
return NewUserError(
&SliceOutOfBoundsError{
startIndex: startIndex,
endIndex: endIndex,
min: min,
max: max,
},
)
}
func (e *SliceOutOfBoundsError) Error() string {
return fmt.Sprintf("slice [%d:%d] is out of bounds with range %d-%d", e.startIndex, e.endIndex, e.min, e.max)
}
// InvalidSliceIndexError is returned when array slice index is invalid, such as startIndex > endIndex
// This error can be returned even when startIndex and endIndex are both within bounds.
type InvalidSliceIndexError struct {
startIndex uint64
endIndex uint64
}
// NewInvalidSliceIndexError constructs an InvalidSliceIndexError
func NewInvalidSliceIndexError(startIndex, endIndex uint64) error {
return NewUserError(
&InvalidSliceIndexError{
startIndex: startIndex,
endIndex: endIndex,
},
)
}
func (e *InvalidSliceIndexError) Error() string {
return fmt.Sprintf("invalid slice index: %d > %d", e.startIndex, e.endIndex)
}
// IndexOutOfBoundsError is returned when get, insert or delete operation is attempted on an array index which is out of bounds
type IndexOutOfBoundsError struct {
index uint64
min uint64
max uint64
}
// NewIndexOutOfBoundsError constructs a IndexOutOfBoundsError
func NewIndexOutOfBoundsError(index, min, max uint64) error {
return NewUserError(
&IndexOutOfBoundsError{
index: index,
min: min,
max: max,
},
)
}
func (e *IndexOutOfBoundsError) Error() string {
return fmt.Sprintf("index %d is outside required range (%d-%d)", e.index, e.min, e.max)
}
// NotValueError is returned when we try to create Value objects from non-root slabs.
type NotValueError struct {
id SlabID
}
// NewNotValueError constructs a NotValueError.
func NewNotValueError(id SlabID) error {
return NewFatalError(&NotValueError{id: id})
}
func (e *NotValueError) Error() string {
return fmt.Sprintf("slab (%s) cannot be used to create Value object", e.id)
}
// DuplicateKeyError is returned when the duplicate key is found in the dictionary when none is expected.
type DuplicateKeyError struct {
key interface{}
}
func NewDuplicateKeyError(key interface{}) error {
return NewFatalError(&DuplicateKeyError{key: key})
}
func (e *DuplicateKeyError) Error() string {
return fmt.Sprintf("duplicate key (%s)", e.key)
}
// KeyNotFoundError is returned when the key not found in the dictionary
type KeyNotFoundError struct {
key interface{}
}
// NewKeyNotFoundError constructs a KeyNotFoundError
func NewKeyNotFoundError(key interface{}) error {
return NewUserError(&KeyNotFoundError{key: key})
}
func (e *KeyNotFoundError) Error() string {
return fmt.Sprintf("key (%s) not found", e.key)
}
// HashSeedUninitializedError is a fatal error returned when hash seed is uninitialized.
type HashSeedUninitializedError struct {
}
func NewHashSeedUninitializedError() error {
return NewFatalError(&HashSeedUninitializedError{})
}
func (e *HashSeedUninitializedError) Error() string {
return "uninitialized hash seed"
}
// HashError is a fatal error returned when hash calculation fails
type HashError struct {
err error
}
// NewHashError constructs a HashError
func NewHashError(err error) error {
return NewFatalError(&HashError{err: err})
}
func (e *HashError) Error() string {
return fmt.Sprintf("hasher error: %s", e.err.Error())
}
// SlabIDError is returned when slab id can't be created or it's invalid.
type SlabIDError struct {
msg string
}
// NewSlabIDError constructs a fatal error of SlabIDError.
func NewSlabIDError(msg string) error {
return NewFatalError(&SlabIDError{msg: msg})
}
// NewSlabIDErrorf constructs a fatal error of SlabIDError.
func NewSlabIDErrorf(msg string, args ...interface{}) error {
return NewSlabIDError(fmt.Sprintf(msg, args...))
}
func (e *SlabIDError) Error() string {
return fmt.Sprintf("slab id error: %s", e.msg)
}
// SlabNotFoundError is always a fatal error returned when an slab is not found
type SlabNotFoundError struct {
slabID SlabID
err error
}
// NewSlabNotFoundError constructs a SlabNotFoundError
func NewSlabNotFoundError(slabID SlabID, err error) error {
return NewFatalError(&SlabNotFoundError{slabID: slabID, err: err})
}
// NewSlabNotFoundErrorf constructs a new SlabNotFoundError with error formating
func NewSlabNotFoundErrorf(slabID SlabID, msg string, args ...interface{}) error {
return NewSlabNotFoundError(slabID, fmt.Errorf(msg, args...))
}
func (e *SlabNotFoundError) Error() string {
return fmt.Sprintf("slab (%s) not found: %s", e.slabID.String(), e.err.Error())
}
// SlabSplitError is always a fatal error returned when splitting an slab has failed
type SlabSplitError struct {
err error
}
// NewSlabSplitError constructs a SlabSplitError
func NewSlabSplitError(err error) error {
return NewFatalError(&SlabSplitError{err: err})
}
// NewSlabSplitErrorf constructs a new SlabSplitError with error formating
func NewSlabSplitErrorf(msg string, args ...interface{}) error {
return NewSlabSplitError(fmt.Errorf(msg, args...))
}
func (e *SlabSplitError) Error() string {
return fmt.Sprintf("slab failed to split: %s", e.err.Error())
}
// SlabMergeError is always a fatal error returned when merging two slabs fails
type SlabMergeError struct {
err error
}
// NewSlabMergeError constructs a SlabMergeError
func NewSlabMergeError(err error) error {
return NewFatalError(&SlabMergeError{err: err})
}
// NewSlabMergeErrorf constructs a new SlabMergeError with error formating
func NewSlabMergeErrorf(msg string, args ...interface{}) error {
return NewSlabMergeError(fmt.Errorf(msg, args...))
}
func (e *SlabMergeError) Error() string {
return fmt.Sprintf("slabs failed to merge: %s", e.err.Error())
}
// SlabRebalanceError is always a fatal error returned when rebalancing a slab has failed
type SlabRebalanceError struct {
err error
}
// NewSlabRebalanceError constructs a SlabRebalanceError
func NewSlabRebalanceError(err error) error {
return NewFatalError(&SlabRebalanceError{err: err})
}
// NewSlabErrorf constructs a new SlabError with error formating
func NewSlabRebalanceErrorf(msg string, args ...interface{}) error {
return NewSlabRebalanceError(fmt.Errorf(msg, args...))
}
func (e *SlabRebalanceError) Error() string {
return fmt.Sprintf("slabs failed to rebalance: %s", e.err.Error())
}
// SlabError is a always fatal error returned when something is wrong with the content or type of the slab
// you can make this a fatal error by calling Fatal()
type SlabDataError struct {
err error
}
// NewSlabDataError constructs a SlabDataError
func NewSlabDataError(err error) error {
return NewFatalError(&SlabDataError{err: err})
}
// NewSlabDataErrorf constructs a new SlabError with error formating
func NewSlabDataErrorf(msg string, args ...interface{}) error {
return NewSlabDataError(fmt.Errorf(msg, args...))
}
func (e *SlabDataError) Error() string {
return fmt.Sprintf("slab data error: %s", e.err.Error())
}
// EncodingError is a fatal error returned when a encoding operation fails
type EncodingError struct {
err error
}
// NewEncodingError constructs a EncodingError
func NewEncodingError(err error) error {
return NewFatalError(&EncodingError{err: err})
}
// NewEncodingErrorf constructs a new EncodingError with error formating
func NewEncodingErrorf(msg string, args ...interface{}) error {
return NewEncodingError(fmt.Errorf(msg, args...))
}
func (e *EncodingError) Error() string {
return fmt.Sprintf("encoding error: %s", e.err.Error())
}
// DecodingError is a fatal error returned when a decoding operation fails
type DecodingError struct {
err error
}
// NewDecodingError constructs a DecodingError
func NewDecodingError(err error) error {
return NewFatalError(&DecodingError{err: err})
}
// NewDecodingErrorf constructs a new DecodingError with error formating
func NewDecodingErrorf(msg string, args ...interface{}) error {
return NewDecodingError(fmt.Errorf(msg, args...))
}
func (e *DecodingError) Error() string {
return fmt.Sprintf("decoding error: %s", e.err.Error())
}
// NotImplementedError is a fatal error returned when a method is called which is not yet implemented
// this is a temporary error
type NotImplementedError struct {
methodName string
}
// NewNotImplementedError constructs a NotImplementedError
func NewNotImplementedError(methodName string) error {
return NewFatalError(&NotImplementedError{methodName: methodName})
}
func (e *NotImplementedError) Error() string {
return fmt.Sprintf("method (%s) is not implemented.", e.methodName)
}
// HashLevelError is a fatal error returned when hash level is wrong.
type HashLevelError struct {
msg string
}
// NewHashLevelError constructs a HashLevelError
func NewHashLevelErrorf(msg string, args ...interface{}) error {
return NewFatalError(&HashLevelError{msg: fmt.Sprintf(msg, args...)})
}
func (e *HashLevelError) Error() string {
return fmt.Sprintf("atree hash level error: %s", e.msg)
}
// NotApplicableError is a fatal error returned when a not applicable method is called
type NotApplicableError struct {
typeName, interfaceName, methodName string
}
// NewNotApplicableError constructs a NotImplementedError
func NewNotApplicableError(typeName, interfaceName, methodName string) error {
return NewFatalError(
&NotApplicableError{
typeName: typeName,
interfaceName: interfaceName,
methodName: methodName,
})
}
func (e *NotApplicableError) Error() string {
return fmt.Sprintf("%s.%s is not applicable for type %s", e.interfaceName, e.methodName, e.typeName)
}
// UnreachableError is used by panic when unreachable code is reached.
// This is copied from Cadence.
type UnreachableError struct {
Stack []byte
}
func NewUnreachableError() error {
return NewFatalError(&UnreachableError{Stack: debug.Stack()})
}
func (e UnreachableError) Error() string {
return fmt.Sprintf("unreachable\n%s", e.Stack)
}
// CollisionLimitError is a fatal error returned when a noncryptographic hash collision
// would exceed collision limit (per digest per map) we enforce in the first level.
type CollisionLimitError struct {
collisionLimitPerDigest uint32 // limit <= 255 is recommended, larger values are useful for tests
}
// NewCollisionLimitError constructs a CollisionLimitError
func NewCollisionLimitError(collisionLimitPerDigest uint32) error {
return NewFatalError(&CollisionLimitError{collisionLimitPerDigest: collisionLimitPerDigest})
}
func (e *CollisionLimitError) Error() string {
return fmt.Sprintf("collision limit per digest %d already reached", e.collisionLimitPerDigest)
}
// MapElementCountError is a fatal error returned when element count is unexpected.
// It is an implemtation error.
type MapElementCountError struct {
msg string
}
// NewMapElementCountError constructs a MapElementCountError.
func NewMapElementCountError(msg string) error {
return NewFatalError(&MapElementCountError{msg: msg})
}
func (e *MapElementCountError) Error() string {
return e.msg
}
// ReadOnlyIteratorElementMutationError is the error returned when readonly iterator element is mutated.
type ReadOnlyIteratorElementMutationError struct {
containerValueID ValueID
elementValueID ValueID
}
// NewReadOnlyIteratorElementMutationError creates ReadOnlyIteratorElementMutationError.
func NewReadOnlyIteratorElementMutationError(containerValueID, elementValueID ValueID) error {
return NewFatalError(&ReadOnlyIteratorElementMutationError{
containerValueID: containerValueID,
elementValueID: elementValueID,
})
}
func (e *ReadOnlyIteratorElementMutationError) Error() string {
return fmt.Sprintf("element (%s) cannot be mutated because it is from readonly iterator of container (%s)", e.elementValueID, e.containerValueID)
}
func wrapErrorAsExternalErrorIfNeeded(err error) error {
return wrapErrorfAsExternalErrorIfNeeded(err, "")
}
func wrapErrorfAsExternalErrorIfNeeded(err error, msg string) error {
if err == nil {
return nil
}
var userError *UserError
var fatalError *FatalError
var externalError *ExternalError
if errors.As(err, &userError) ||
errors.As(err, &fatalError) ||
errors.As(err, &externalError) {
// No-op if err is already categorized.
return err
}
// Create new external error wrapping err with context.
return NewExternalError(err, msg)
}