-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.go
640 lines (567 loc) · 15.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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
// Package errors provides a custom error type for out-of-bound errors.
package Errors
import (
"fmt"
"reflect"
"strings"
)
// ErrNoError represents an error when no error occurs.
type ErrNoError struct {
// Err is the reason for the no error error.
Err error
}
// ErrorIf is a method of that returns the error if the error is not nil.
//
// Returns:
// - error: The error if the error is not nil, nil otherwise.
func (e *ErrNoError) ErrorIf() error {
if e.Err != nil {
return e
}
return nil
}
// Error is a method of the error interface.
//
// Returns:
// - string: The error message of the no error error
// (no mention of being a no error error).
func (e *ErrNoError) Error() string {
if e.Err == nil {
return "no error"
} else {
return e.Err.Error()
}
}
// Unwrap is a method of the errors interface.
//
// Returns:
// - error: The reason for the no error error.
func (e *ErrNoError) Unwrap() error {
return e.Err
}
// ChangeReason changes the reason of the no error error.
//
// Parameters:
// - reason: The new reason for the no error error.
func (e *ErrNoError) ChangeReason(reason error) {
e.Err = reason
}
// NewErrNoError creates a new ErrNoError error.
//
// Parameters:
// - err: The reason for the no error error.
//
// Returns:
// - *ErrNoError: A pointer to the newly created ErrNoError.
func NewErrNoError(err error) *ErrNoError {
return &ErrNoError{Err: err}
}
// ErrInvalidParameter represents an error when a parameter is invalid.
type ErrInvalidParameter struct {
// Parameter is the name of the Parameter.
Parameter string
// Reason is the Reason for the invalidity of the parameter.
Reason error
}
// Error is a method of the error interface.
//
// If the reason is not provided (nil), no reason is included in the error message.
//
// Returns:
// - string: The error message.
func (e *ErrInvalidParameter) Error() string {
if e.Reason == nil {
return fmt.Sprintf("parameter (%s) is invalid", e.Parameter)
} else {
return fmt.Sprintf("parameter (%s) is invalid: %s", e.Parameter, e.Reason.Error())
}
}
// Unwrap returns the reason for the invalidity of the parameter.
// It is used for error unwrapping.
//
// Returns:
// - error: The reason for the invalidity of the parameter.
func (e *ErrInvalidParameter) Unwrap() error {
return e.Reason
}
// ChangeReason changes the reason for the invalidity of the parameter.
//
// Parameters:
// - reason: The new reason for the invalidity of the parameter.
func (e *ErrInvalidParameter) ChangeReason(reason error) {
e.Reason = reason
}
// NewErrInvalidParameter creates a new ErrInvalidParameter error.
// If the reason is not provided (nil), the reason is set to
// "parameter is invalid" by default.
//
// Parameters:
// - parameter: The name of the parameter.
// - reason: The reason for the invalidity.
//
// Returns:
// - error: A pointer to the newly created ErrInvalidParameter.
func NewErrInvalidParameter(parameter string, reason error) *ErrInvalidParameter {
return &ErrInvalidParameter{
Parameter: parameter,
Reason: reason,
}
}
// ErrNilParameter represents an error when a parameter is nil.
// This is a shorthand for NewErrInvalidParameter(parameter, NewErrNilValue()).
//
// Parameters:
// - parameter: The name of the parameter.
//
// Returns:
// - *ErrInvalidParameter: A pointer to the newly created ErrInvalidParameter.
func NewErrNilParameter(parameter string) *ErrInvalidParameter {
return &ErrInvalidParameter{
Parameter: parameter,
Reason: NewErrNilValue(),
}
}
// ErrInvalidCall represents an error that occurs when a function
// is not called correctly.
type ErrInvalidCall struct {
// FnName is the name of the function.
FnName string
// Signature is the Signature of the function.
Signature reflect.Type
// Reason is the Reason for the failure.
Reason error
}
// Error is a method of the error interface.
//
// Returns:
//
// - string: The error message.
func (e *ErrInvalidCall) Error() string {
var builder strings.Builder
builder.WriteString("call")
builder.WriteRune(' ')
builder.WriteString("to")
builder.WriteRune(' ')
builder.WriteString(e.FnName)
builder.WriteString(e.Signature.String())
builder.WriteRune(' ')
builder.WriteString("failed")
if e.Reason != nil {
builder.WriteRune(':')
builder.WriteRune(' ')
builder.WriteString(e.Reason.Error())
}
return builder.String()
}
// Unwrap returns the underlying error that caused the ErrInvalidCall.
// It is used for error unwrapping.
//
// Returns:
// - error: The reason for the failure.
func (e *ErrInvalidCall) Unwrap() error {
return e.Reason
}
// ChangeReason changes the reason for the failure of the function call.
//
// Parameters:
// - reason: The new reason for the failure.
func (e *ErrInvalidCall) ChangeReason(reason error) {
e.Reason = reason
}
// NewErrInvalidCall creates a new ErrInvalidCall. If the reason is not provided (nil),
// the reason is set to "an error occurred while calling the function" by default.
//
// Parameters:
// - functionName: The name of the function.
// - function: The function that failed.
// - reason: The reason for the failure.
//
// Returns:
// - *ErrInvalidCall: A pointer to the new ErrInvalidCall.
func NewErrInvalidCall(functionName string, function any, reason error) *ErrInvalidCall {
return &ErrInvalidCall{
FnName: functionName,
Signature: reflect.ValueOf(function).Type(),
Reason: reason,
}
}
// ErrIgnorable represents an error that can be ignored. Useful for indicating
// that an error is ignorable.
type ErrIgnorable struct {
// Err is the error that can be ignored.
Err error
}
// Error is a method of the error interface.
// It does not mention that the error is ignorable.
//
// Returns:
//
// - string: The error message of the ignorable error (no mention of being ignorable).
func (e *ErrIgnorable) Error() string {
if e.Err == nil {
return "ignorable error"
} else {
return e.Err.Error()
}
}
// Unwrap returns the error that can be ignored.
// It is used for error unwrapping.
//
// Returns:
// - error: The error that can be ignored.
func (e *ErrIgnorable) Unwrap() error {
return e.Err
}
// ChangeReason changes the reason for the ignorable error.
//
// Parameters:
// - reason: The new reason for the ignorable error.
func (e *ErrIgnorable) ChangeReason(reason error) {
e.Err = reason
}
// NewErrIgnorable creates a new ErrIgnorable error.
//
// Parameters:
// - err: The error that can be ignored.
//
// Returns:
// - *ErrIgnorable: A pointer to the newly created ErrIgnorable.
func NewErrIgnorable(err error) *ErrIgnorable {
return &ErrIgnorable{
Err: err,
}
}
// ErrInvalidRune represents an error when an invalid rune is encountered.
type ErrInvalidRune struct {
// Reason is the reason for the invalidity of the rune.
Reason error
}
// Error is a method of the error interface that returns the error message.
//
// If the reason is not provided (nil), no reason is included in the error message.
//
// Returns:
// - string: The error message.
func (e *ErrInvalidRune) Error() string {
if e.Reason == nil {
return "rune is invalid"
} else {
return fmt.Sprintf("invalid rune: %s", e.Reason.Error())
}
}
// Unwrap returns the reason for the invalidity of the rune.
// It is used for error unwrapping.
//
// Returns:
// - error: The reason for the invalidity of the rune.
func (e *ErrInvalidRune) Unwrap() error {
return e.Reason
}
// ChangeReason changes the reason for the invalidity of the rune.
//
// Parameters:
// - reason: The new reason for the invalidity of the rune.
func (e *ErrInvalidRune) ChangeReason(reason error) {
e.Reason = reason
}
// NewErrInvalidRune creates a new ErrInvalidRuneAt error.
//
// Parameters:
// - reason: The reason for the invalidity of the rune.
//
// Returns:
// - *ErrInvalidRune: A pointer to the newly created ErrInvalidRune.
func NewErrInvalidRune(reason error) *ErrInvalidRune {
return &ErrInvalidRune{
Reason: reason,
}
}
// ErrAt represents an error that occurs at a specific index.
type ErrAt struct {
// Index is the index where the error occurred.
Index int
// Reason is the reason for the error.
Reason error
}
// Error is a method of the error interface that returns the error message.
//
// If the reason is not provided (nil), the error message is
// "at index %d: something went wrong".
//
// Returns:
// - string: The error message.
func (e *ErrAt) Error() string {
if e.Reason == nil {
return fmt.Sprintf("something went wrong at index %d", e.Index)
} else {
return fmt.Sprintf("at index %d: %s", e.Index, e.Reason.Error())
}
}
// Unwrap returns the reason for the error.
// It is used for error unwrapping.
//
// Returns:
// - error: The reason for the error.
func (e *ErrAt) Unwrap() error {
return e.Reason
}
// ChangeReason changes the reason for the error.
//
// Parameters:
// - reason: The new reason for the error.
func (e *ErrAt) ChangeReason(reason error) {
e.Reason = reason
}
// NewErrAt creates a new ErrAt error.
//
// Parameters:
// - index: The index where the error occurred.
// - reason: The reason for the error.
//
// Returns:
// - *ErrAt: A pointer to the newly created ErrAt.
func NewErrAt(index int, reason error) *ErrAt {
return &ErrAt{
Index: index,
Reason: reason,
}
}
// ErrAfter is an error that is returned when something goes wrong after a certain
// element in a stream of data.
type ErrAfter struct {
// After is the element that was processed before the error occurred.
After string
// Reason is the reason for the error.
Reason error
}
// Error is a method of the error interface.
//
// Returns:
// - string: The error message.
func (e *ErrAfter) Error() string {
if e.Reason == nil {
return fmt.Sprintf("something went wrong after %s", e.After)
} else {
return fmt.Sprintf("after %s: %s", e.After, e.Reason.Error())
}
}
// Unwrap returns the reason for the error.
// It is used for error unwrapping.
//
// Returns:
// - error: The reason for the error.
func (e *ErrAfter) Unwrap() error {
return e.Reason
}
// ChangeReason changes the reason for the error.
//
// Parameters:
// - reason: The new reason for the error.
func (e *ErrAfter) ChangeReason(reason error) {
e.Reason = reason
}
// NewErrAfter creates a new ErrAfter error.
//
// Parameters:
// - after: The element that was processed before the error occurred.
// - reason: The reason for the error.
//
// Returns:
// - *ErrAfter: A pointer to the new ErrAfter error.
func NewErrAfter(after string, reason error) *ErrAfter {
return &ErrAfter{
After: after,
Reason: reason,
}
}
// ErrBefore is an error that is returned when something goes wrong before
// a certain element in a stream of data.
type ErrBefore struct {
// Before is the element that was processed before the error occurred.
Before string
// Reason is the reason for the error.
Reason error
}
// Error is a method of the error interface.
//
// Returns:
// - string: The error message.
func (e *ErrBefore) Error() string {
if e.Reason == nil {
return fmt.Sprintf("something went wrong before %s", e.Before)
} else {
return fmt.Sprintf("before %s: %s", e.Before, e.Reason.Error())
}
}
// Unwrap returns the reason for the error.
// It is used for error unwrapping.
//
// Returns:
// - error: The reason for the error.
func (e *ErrBefore) Unwrap() error {
return e.Reason
}
// ChangeReason changes the reason for the error.
//
// Parameters:
// - reason: The new reason for the error.
func (e *ErrBefore) ChangeReason(reason error) {
e.Reason = reason
}
// NewErrBefore creates a new ErrBefore error.
//
// Parameters:
// - before: The element that was processed before the error occurred.
// - reason: The reason for the error.
//
// Returns:
// - *ErrBefore: A pointer to the new ErrBefore error.
func NewErrBefore(before string, reason error) *ErrBefore {
return &ErrBefore{
Before: before,
Reason: reason,
}
}
// ErrInvalidUsage represents an error that occurs when a function is used incorrectly.
type ErrInvalidUsage struct {
// Reason is the reason for the invalid usage.
Reason error
// Usage is the usage of the function.
Usage string
}
// Error is a method of the error interface.
//
// Returns:
// - string: The error message.
func (e *ErrInvalidUsage) Error() string {
var reason string
if e.Reason == nil {
reason = "invalid usage"
} else {
reason = e.Reason.Error()
}
if e.Usage == "" {
return reason
}
return fmt.Sprintf("%s. %s", reason, e.Usage)
}
// Unwrap returns the reason for the invalid usage.
// It is used for error unwrapping.
//
// Returns:
// - error: The reason for the invalid usage.
func (e *ErrInvalidUsage) Unwrap() error {
return e.Reason
}
// ChangeReason changes the reason for the invalid usage.
//
// Parameters:
// - reason: The new reason for the invalid usage.
func (e *ErrInvalidUsage) ChangeReason(reason error) {
e.Reason = reason
}
// NewErrInvalidUsage creates a new ErrInvalidUsage error.
//
// Parameters:
// - reason: The reason for the invalid usage.
// - usage: The usage of the function.
//
// Returns:
// - *ErrInvalidUsage: A pointer to the new ErrInvalidUsage error.
func NewErrInvalidUsage(reason error, usage string) *ErrInvalidUsage {
return &ErrInvalidUsage{
Reason: reason,
Usage: usage,
}
}
// ErrUnexpectedError represents an error that occurs unexpectedly.
type ErrUnexpectedError struct {
// Reason is the reason for the unexpected error.
Reason error
}
// Error is a method of the error interface.
//
// Returns:
// - string: The error message.
func (e *ErrUnexpectedError) Error() string {
if e.Reason == nil {
return "unexpected error"
} else {
return fmt.Sprintf("unexpected error: %s", e.Reason.Error())
}
}
// Unwrap returns the reason for the unexpected error.
// It is used for error unwrapping.
//
// Returns:
// - error: The reason for the unexpected error.
func (e *ErrUnexpectedError) Unwrap() error {
return e.Reason
}
// ChangeReason changes the reason for the unexpected error.
//
// Parameters:
// - reason: The new reason for the unexpected error.
func (e *ErrUnexpectedError) ChangeReason(reason error) {
e.Reason = reason
}
// NewErrUnexpectedError creates a new ErrUnexpectedError error.
//
// Parameters:
// - reason: The reason for the unexpected error.
//
// Returns:
// - *ErrUnexpectedError: A pointer to the new ErrUnexpectedError error.
func NewErrUnexpectedError(reason error) *ErrUnexpectedError {
return &ErrUnexpectedError{
Reason: reason,
}
}
// ErrVariableError represents an error that occurs when a variable is invalid.
type ErrVariableError struct {
// Variable is the name of the variable that caused the error.
Variable string
// Reason is the reason for the variable error.
Reason error
}
// Error returns the error message: "variable (<variable>) error"
// or "variable (<variable>) error: <reason>" if the reason is provided.
//
// Returns:
// - string: The error message.
func (e *ErrVariableError) Error() string {
if e.Reason == nil {
return fmt.Sprintf("variable (%s) error", e.Variable)
} else {
return fmt.Sprintf("variable (%s) error: %s", e.Variable, e.Reason.Error())
}
}
// Unwrap returns the reason for the variable error.
// It is used for error unwrapping.
//
// Returns:
// - error: The reason for the variable error.
func (e *ErrVariableError) Unwrap() error {
return e.Reason
}
// ChangeReason changes the reason for the variable error.
//
// Parameters:
// - reason: The new reason for the variable error.
func (e *ErrVariableError) ChangeReason(reason error) {
e.Reason = reason
}
// NewErrVariableError creates a new ErrVariableError error.
//
// Parameters:
// - variable: The name of the variable that caused the error.
// - reason: The reason for the variable error.
//
// Returns:
// - *ErrVariableError: A pointer to the new ErrVariableError error.
func NewErrVariableError(variable string, reason error) *ErrVariableError {
return &ErrVariableError{
Variable: variable,
Reason: reason,
}
}