-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
599 lines (502 loc) · 16.8 KB
/
Copy pathexample_test.go
File metadata and controls
599 lines (502 loc) · 16.8 KB
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
// Copyright 2021 The customerror Authors. All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.
package customerror
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"net/http"
"strings"
"time"
"github.com/eapache/go-resiliency/retrier"
)
func checkIfStringContainsMany(s string, subs ...string) []string {
missing := []string{}
for _, sub := range subs {
if !strings.Contains(s, sub) {
missing = append(missing, sub)
}
}
return missing
}
// Demonstrates how to create static, and dynamic custom errors, also how to
// check, and instrospect custom errors.
func ExampleNew() {
// Custom static error definition.
ErrMissingID := NewMissingError("id", WithErrorCode("E1010"))
// Some function, for demo purpose.
SomeFunc := func(id string) error {
if id == "" {
// Usage of the custom static error.
return ErrMissingID
}
// Dynamic custom error.
return NewFailedToError("write to disk", WithErrorCode("E1523"))
}
// Case: Without `id`, returns `ErrMissingID`.
if err := SomeFunc(""); err != nil {
fmt.Println(errors.Is(err, ErrMissingID)) // true
var cE *CustomError
if errors.As(err, &cE) {
fmt.Println(cE.StatusCode) // 400
}
fmt.Println(err) // E1010: missing id (400 - Bad Request)
}
// Case: With `id`, returns dynamic error.
if err := SomeFunc("12345"); err != nil {
var cE *CustomError
if errors.As(err, &cE) {
fmt.Println(cE.StatusCode) // 500
}
fmt.Println(err) // E1523: failed to write to disk (500 - Internal Server Error)
}
// output:
// true
// 400
// E1010: missing id
// 500
// E1523: failed to write to disk
}
// Demonstrates how to create static, and dynamic custom errors, also how to
// check, and instrospect custom errors.
//
//nolint:errorlint,forcetypeassert
func ExampleNew_options() {
fmt.Println(
NewMissingError("id", WithErrorCode("E1010"), WithStatusCode(http.StatusNotAcceptable), WithError(errors.New("some error"))).(*CustomError).APIError(),
)
// output:
// E1010: missing id (406 - Not Acceptable). Original Error: some error
}
// Demonstrates error chain. `errB` will wrap `errA` and will be considered the
// same by propagating the chain.
func ExampleNew_is() {
errA := NewMissingError("id")
errB := NewMissingError("name", WithError(errA))
fmt.Println(errors.Is(errB, errA))
// output:
// true
}
// Demonstrates JSON marshalling of custom errors.
func ExampleNew_marshalJSON() {
// New buffer string.
var buf strings.Builder
errA := NewMissingError("id")
errB := NewMissingError("name", WithError(errA))
if err := json.NewEncoder(&buf).Encode(errB); err != nil {
panic(err)
}
fmt.Println(strings.Contains(buf.String(), `message":"missing name. Original Error: missing id`))
// output:
// true
}
// Demonstrates the WithIgnoreString option.
func ExampleNew_optionsWithIgnoreString() {
fmt.Println(NewMissingError("id", WithIgnoreString("id")) == nil)
fmt.Println(NewMissingError("id", WithError(errors.New("hehehe")), WithIgnoreString("hehehe")) == nil)
fmt.Println(NewMissingError("id", WithIgnoreString("hahaha")) == nil)
// output:
// true
// true
// false
}
// Demonstrates the WithIgnoreFunc option.
func ExampleNew_optionsWithIgnoreIf() {
fmt.Println(NewMissingError("id", WithIgnoreFunc(func(cE *CustomError) bool {
return strings.Contains(cE.Message, "id")
})) == nil)
// output:
// true
}
// Demonstrates the NewHTTPError custom error.
//
//nolint:errorlint,forcetypeassert
func ExampleNew_newHTTPError() {
fmt.Println(NewHTTPError(http.StatusNotFound).(*CustomError).APIError())
fmt.Println(NewHTTPError(http.StatusNotFound).(*CustomError).Error())
fmt.Println(NewHTTPError(http.StatusNotFound))
// output:
// not found (404 - Not Found)
// not found
// not found
}
// Demonstrates errors without message but with status code.
//
//nolint:errorlint,forcetypeassert
func ExampleNew_newNoMessage() {
fmt.Println(New("", WithStatusCode(http.StatusAccepted)))
fmt.Println(New("", WithStatusCode(http.StatusAccepted), WithErrorCode("E1010")))
fmt.Println(New("", WithStatusCode(http.StatusAccepted)).(*CustomError).APIError())
fmt.Println(New("", WithStatusCode(http.StatusAccepted), WithErrorCode("E1010")).(*CustomError).APIError())
fmt.Println(New("", WithErrorCode("E1010")))
fmt.Println(New("", WithErrorCode("E1010"), WithStatusCode(http.StatusAccepted)))
fmt.Println(New("", WithErrorCode("E1010")).(*CustomError).APIError())
fmt.Println(New("", WithErrorCode("E1010"), WithStatusCode(http.StatusAccepted)).(*CustomError).APIError())
// output:
// Accepted
// E1010: Accepted
// Accepted (202)
// E1010: Accepted (202)
// E1010
// E1010: Accepted
// E1010
// E1010: Accepted (202)
}
// Demonstrates the WithTag option.
//
//nolint:errorlint,forcetypeassert
func ExampleNew_optionsWithTag() {
fmt.Println(NewMissingError(
"id",
WithTag("test1", "test2"),
WithErrorCode("E1010"),
WithStatusCode(http.StatusNotAcceptable),
WithError(errors.New("some error")),
))
fmt.Println(NewMissingError(
"id",
WithTag("test1", "test2"),
WithErrorCode("E1010"),
WithStatusCode(http.StatusNotAcceptable),
WithError(errors.New("some error")),
).(*CustomError).APIError())
// output:
// E1010: missing id. Original Error: some error. Tags: test1, test2
// E1010: missing id (406 - Not Acceptable). Original Error: some error. Tags: test1, test2
}
// Demonstrates the WithFields option.
//
//nolint:errorlint,forcetypeassert
func ExampleNew_optionsWithFields() {
// Store message in a buf to be checked later.
var buf strings.Builder
fmt.Fprintln(&buf, NewMissingError(
"id",
WithTag("test1", "test2"),
WithFields(map[string]interface{}{
"testKey1": "testValue1",
"testKey2": "testValue2",
}),
WithErrorCode("E1010"),
WithStatusCode(http.StatusNotAcceptable),
WithError(errors.New("some error")),
))
fmt.Fprintln(&buf, NewMissingError(
"id",
WithTag("test1", "test2"),
WithFields(map[string]interface{}{
"testKey1": "testValue1",
"testKey2": "testValue2",
}),
WithErrorCode("E1010"),
WithStatusCode(http.StatusNotAcceptable),
WithError(errors.New("some error")),
).(*CustomError).APIError())
fmt.Println(len(checkIfStringContainsMany(
buf.String(),
"E1010: missing id",
"E1010: missing id (406 - Not Acceptable)",
"Original Error: some error",
"Tags: test1, test2",
"Fields:",
"testKey1=testValue1",
"testKey2=testValue2",
)) == 0)
// output:
// true
}
// Demonstrates the WithField option.
//
//nolint:errorlint,forcetypeassert
func ExampleNew_optionsWithField() {
// Store message in a buf to be checked later.
var buf strings.Builder
fmt.Fprintln(&buf, NewMissingError(
"id",
WithTag("test1", "test2"),
WithField("testKey1", "testValue1"),
WithField("testKey2", "testValue2"),
WithErrorCode("E1010"),
WithStatusCode(http.StatusNotAcceptable),
WithError(errors.New("some error")),
))
fmt.Fprintln(&buf, NewMissingError(
"id",
WithTag("test1", "test2"),
WithField("testKey1", "testValue1"),
WithField("testKey2", "testValue2"),
WithErrorCode("E1010"),
WithStatusCode(http.StatusNotAcceptable),
WithError(errors.New("some error")),
).(*CustomError).APIError())
fmt.Println(len(checkIfStringContainsMany(
buf.String(),
"E1010: missing id",
"E1010: missing id (406 - Not Acceptable)",
"Original Error: some error",
"Tags: test1, test2",
"Fields:",
"testKey1=testValue1",
"testKey2=testValue2",
)) == 0)
// output:
// true
}
// ExampleNew_NewFactory demonstrates the usage of the NewFactory function.
func ExampleNew_newFactory() {
factory := Factory(
"id",
WithFields(map[string]interface{}{
"test1": "test2",
"test3": "test4",
}),
WithTag("testTag1", "testTag2", "testTag3"),
)
childFactory := factory.NewChildError(
WithFields(map[string]interface{}{
"test1": "test2",
"test5": "test6",
}),
WithTag("testTag2", "testTag3", "testTag4"),
)
// Write to a buffer and check the output.
var buf bytes.Buffer
fmt.Fprint(&buf, childFactory.NewMissingError())
fmt.Fprint(&buf, childFactory.NewFailedToError())
fmt.Fprint(&buf, childFactory.NewInvalidError())
fmt.Fprint(&buf, childFactory.NewRequiredError())
fmt.Fprint(&buf, childFactory.NewHTTPError(400))
finalMessage := buf.String()
missing := checkIfStringContainsMany(
finalMessage,
"missing id", "failed to id", "invalid id", "missing id", "id required", "bad request",
"Tags:", "Fields:", "testTag1", "testTag2", "testTag3", "testTag4",
"test1=test2", "test3=test4", "test5=test6",
)
if len(missing) == 0 {
fmt.Println(true)
} else {
fmt.Println(missing)
}
// output:
// true
}
// ExampleNew_i18n demonstrates how to create an error catalog with translations
// and how to throw errors in different languages.
//
// SEE: `i18n.md` file for more information.
func ExampleNew_i18n() {
//////
// The following, is usually defined in the `errorcatalog.go` file.
//////
const (
//////
// Define the error code constant. It helps to identify the error in
// systems like Elasticsearch, Splunk and Datadog. It also helps to
// maintain consistency across the application.
//////
ErrInvalidHardDrivePath = "ERR_INVALID_HARD_DRIVE_PATH"
)
// Create the application error catalog.
catalog := MustNewCatalog("myIncredibleApp").
// Add errors by their constant error codes while setting up the
// translations. For Spanish and French, it uses the built-in list of
// common languages instead of hardcoding the language.
//
// NOTE: For supported built-in languages, the default word(s) used by
// the built-in error functions (example: `NewFailedToError`), are
// automatically translated and included in the message.
//
// SEE: `languages.go` file an up-to-date list of the supported built-in
// list of languages.
//
// For ANY other language there are two options:
// 1. Don't use the built-in functions but instead use the `New` function
// and write the message in full, for example: "invalid hard drive path".
// 2. Setup the language (see `ExampleNew_i18nSetupNewLang`).
//
// Reason: It's impossible for any package to cover all the possible
// languages, combinations, and their translations.
//
// No need to add the "invalid" word.
MustSet(ErrInvalidHardDrivePath, "hard drive path",
// No need to add the "invalid" word.
WithTranslation(Spanish.String(), "ruta de disco duro"),
// No need to add the "invalid" word.
WithTranslation(French.String(), "chemin de disque dur"),
)
//////
// The following, from anywhere in the application.
//////
// Retrieve the error from the catalog.
err := catalog.MustGet(ErrInvalidHardDrivePath)
// Throw that in the Spanish language as an and using the built-in
// `InvalidError` function which automatically sets the HTTP status code to
// `StatusBadRequest`. The language is specified by using the built-in list
// of languages. The default "invalid" word is automatically translated.
//
// SEE: `languages.go` file for the list of languages.
fmt.Println(err.NewInvalidError(WithLanguage(Spanish.String())))
// The same, but in French.
fmt.Println(err.NewInvalidError(WithLanguage(French.String())))
// The same, standard way - in English.
fmt.Println(err.NewInvalidError())
// output:
// ruta de disco duro inválido
// chemin de disque dur invalide
// invalid hard drive path
}
// ExampleNew_i18n demonstrates how to create an error catalog with translations
// how to throw errors in different languages, and how to setup a new language.
//
// SEE: `i18n.md` file for more information.
func ExampleNew_i18nSetupNewLang() {
//////
// The following, is usually defined in the `errorcatalog.go` file.
//////
// Define a constant for the new language, to help with consistency.
const Japanase = "jp"
// Let's pretend that beyond English, Spanish, and French, the
// application must support Japanese. In this case, Japanase is not part
// of the built-in list of languages. We start by setting up the language,
// and the respective built-in functions error messages.
//
// NOTE: MustAddNewLanguage properly updates the internal, package-level,
// singleton.
//
// WARN: If you use `MustAddNewLanguage`, and specify an invalid language
// such as "asd", it will panic! The language must be a valid ISO 639-1 or
// ISO 3166-1 alpha-2.
MustAddNewLanguage("jp", NewErrorPrefixMap(
// "failed to" template.
"%s に失敗しました",
// "invalid" template.
"%s が無効です",
// "missing" template.
"%s が見つかりません",
// "required" template.
"%s が必要です",
// "not found" template.
"%s が見つかりませんでした",
))
const (
//////
// Define the error code constant. It helps to identify the error in
// systems like Elasticsearch, Splunk and Datadog. It also helps to
// maintain consistency across the application.
//////
ErrInvalidHardDrivePath = "ERR_INVALID_HARD_DRIVE_PATH"
)
// Create the application error catalog.
catalog := MustNewCatalog("myIncredibleApp").
// Add errors by their constant error codes while setting up the
// translations. For Spanish and French, it uses the built-in list of
// common languages instead of hardcoding the language.
//
// NOTE: For supported built-in languages, the default word(s) used by
// the built-in error functions (example: `NewFailedToError`), are
// automatically translated and included in the message.
//
// SEE: `languages.go` file an up-to-date list of the supported built-in
// list of languages.
//
// For ANY other language there are two options:
// 1. Don't use the built-in functions but instead use the `New` function
// and write the message in full, for example: "invalid hard drive path".
// 2. Setup the language (see `ExampleNew_i18nSetupNewLang`).
//
// Reason: It's impossible for any package to cover all the possible
// languages, combinations, and their translations.
//
// No need to add the "invalid" word.
MustSet(ErrInvalidHardDrivePath, "hard drive path",
// No need to add the "invalid" word.
WithTranslation(Spanish.String(), "ruta de disco duro"),
// No need to add the "invalid" word.
WithTranslation(French.String(), "chemin de disque dur"),
// Added support, no need to add the "invalid" word.
WithTranslation("jp", "ハードドライブのパス"),
)
// Retrieve the error from the catalog.
err := catalog.MustGet(ErrInvalidHardDrivePath)
// Throw that in the Spanish language as an and using the built-in
// `InvalidError` function which automatically sets the HTTP status code to
// `StatusBadRequest`. The language is specified by using the built-in list
// of languages. The default "invalid" word is automatically translated.
//
// SEE: `languages.go` file for the list of languages.
fmt.Println(err.NewInvalidError(WithLanguage(Spanish.String())))
// The same, but in French.
fmt.Println(err.NewInvalidError(WithLanguage(French.String())))
// The same, but in Japanese.
fmt.Println(err.NewInvalidError(WithLanguage(Japanase)))
// The same, standard way - in English.
fmt.Println(err.NewInvalidError())
// output:
// ruta de disco duro inválido
// chemin de disque dur invalide
// ハードドライブのパス が無効です
// invalid hard drive path
}
type CustomClassifier struct{}
// Classify implements the Classifier interface.
func (hSCC CustomClassifier) Classify(err error) retrier.Action {
// Should do nothing if there's no error.
if err == nil {
return retrier.Succeed
}
//////
// Cast error into customerror to evaluate if the error is retryable.
//////
var cE *CustomError
if errors.As(err, &cE) {
if cE.Retryable {
// Update the error to be retried.
cE.SetRetried(true)
return retrier.Retry
}
}
// Should fail for everything else.
return retrier.Fail
}
// ExampleNew_Retryable error and SetRetried function. In this example the go
// go-resiliency/retrier package will be used, but any other retry package
// should work as well. Additionally, the example demonstrates how to check if
// the error is retryable, if it has a specific error code, if it has a specific
// HTTP status code, and if it is a custom error.
func ExampleNew_newRetryableError() {
// Create a custom retryable error.
retryableCE := NewFailedToError(
"write to disk",
WithErrorCode("E1523"),
WithRetryable(true),
)
// Initialize a retrier with exponential backoff strategy.
r1 := retrier.New(
retrier.ConstantBackoff(1, 300*time.Millisecond),
CustomClassifier{},
)
// Execute the request with retry logic.
if err := r1.Run(func() error {
// Throw the retryable error.
return retryableCE
}); err != nil {
fmt.Println(err)
}
fmt.Println(IsRetryable(retryableCE))
fmt.Println(IsErrorCode(retryableCE, "E1523"))
fmt.Println(IsHTTPStatus(retryableCE, http.StatusInternalServerError))
fmt.Println(IsCustomError(retryableCE))
if toCE, ok := To(retryableCE); ok {
fmt.Println(toCE.StatusCode)
}
// output:
// E1523: failed to write to disk. Retryable: true. Retried: true
// true
// true
// true
// true
// 500
}