-
Notifications
You must be signed in to change notification settings - Fork 0
/
error_example_test.go
121 lines (92 loc) · 2.45 KB
/
error_example_test.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
package zrr_test
import (
"errors"
"fmt"
"time"
"github.com/rzajac/zrr"
)
func ExampleError() {
// Create an error and add a bunch of context fields to it.
err := zrr.Wrap(errors.New("std error"), "ECode").
Str("str", "string").
Int("int", 5).
Float64("float64", 1.23).
Time("time", time.Date(2020, time.October, 7, 23, 47, 0, 0, time.UTC)).
Bool("bool", true)
fmt.Println(err.Error())
fmt.Println(zrr.GetStr(err, "str"))
fmt.Println(zrr.GetInt(err, "int"))
fmt.Println(zrr.GetFloat64(err, "float64"))
fmt.Println(zrr.GetTime(err, "time"))
fmt.Println(zrr.GetBool(err, "bool"))
fmt.Println(err.ErrCode())
// Output:
// std error
// string true
// 5 true
// 1.23 true
// 2020-10-07 23:47:00 +0000 UTC true
// true true
// ECode
}
func ExampleError_wrappingZrrError() {
err := zrr.New("my error").Str("key", "value")
e1 := fmt.Errorf("zrr wrapped: %w", err)
fmt.Println(e1)
fmt.Println(errors.Is(e1, err))
// Output:
// zrr wrapped: my error
// true
}
func ExampleImm() {
// Create immutable error.
var ErrPackageLevel = zrr.Imm("package level error", "ECode")
// Somewhere in the code use ErrPackageLevel and add context to it.
err := zrr.Wrap(ErrPackageLevel, "ENewCode").Str("path", "/path/to/file")
fmt.Println(ErrPackageLevel, zrr.GetCode(ErrPackageLevel)) // Notice the error code has not been changed.
fmt.Println(err, zrr.GetCode(err))
fmt.Println(errors.Is(err, ErrPackageLevel))
// Output:
// package level error ECode
// package level error ENewCode
// true
}
func ExampleWrap() {
err := errors.New("some error")
e1 := zrr.Wrap(err).Str("key", "value")
fmt.Println(e1)
fmt.Println(errors.Is(e1, err))
// Output:
// some error
// true
}
func ExampleGetCode() {
err := zrr.New("message", "ECode").Int("retry", 5)
fmt.Println(zrr.GetCode(err))
// Output: ECode
}
func ExampleGetInt() {
var err error
err = zrr.New("message", "ECode").Int("retry", 5)
fmt.Println(zrr.GetInt(err, "retry")) // 5 true
fmt.Println(zrr.GetInt(err, "not_set")) // 0 false
fmt.Println(zrr.HasKey(err, "retry")) // true
fmt.Println(zrr.HasKey(err, "not_set")) // false
// Output:
// 5 true
// 0 false
// true
// false
}
func ExampleHasKey() {
err := zrr.New("message", "ECode").Int("retry", 5)
fmt.Println(zrr.GetInt(err, "retry"))
fmt.Println(zrr.GetInt(err, "not_set"))
// Output: 5 true
// 0 false
}
func ExampleError_Error() {
err := zrr.New("message").Int("retry", 5)
fmt.Println(err.Error())
// Output: message
}