-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexamples_test.go
196 lines (164 loc) · 4.55 KB
/
examples_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
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
package errortree_test
import (
"errors"
"fmt"
"strings"
"github.com/speijnik/go-errortree"
)
func ExampleAdd() {
var err error
// Using Add on a nil-error automatically creates an error tree and adds the desired error
err = errortree.Add(err, "test", errors.New("test error"))
fmt.Println(err.Error())
// Output: 1 error occurred:
//
// * test: test error
}
func ExampleAdd_nested() {
// Create an error which will acts as the child for our top-level error
childError := errortree.Add(nil, "test0", errors.New("child error"))
// Add another error to our child
childError = errortree.Add(childError, "test1", errors.New("another child error"))
// Create the top-level error, adding the child in the process
err := errortree.Add(nil, "child", childError)
// Add another top-level error
err = errortree.Add(err, "second", errors.New("top-level error"))
fmt.Println(err.Error())
// Output: 3 errors occurred:
//
// * child:test0: child error
// * child:test1: another child error
// * second: top-level error
}
func ExampleAdd_duplicate() {
// Add an error with the key "test"
err := errortree.Add(nil, "test", errors.New("test error"))
// Recover from the panic, this will the output we expect below
defer func() {
if r := recover(); r != nil {
fmt.Println(r)
}
}()
errortree.Add(err, "test", errors.New("key re-used"))
// Output: Cannot add error: key test exists.
}
func ExampleSet() {
var err error
// Using Set on a nil-error automatically creates an error tree and adds the desired error
err = errortree.Set(err, "test", errors.New("test error"))
fmt.Println(err.Error())
// Output: 1 error occurred:
//
// * test: test error
}
func ExampleSet_duplicate() {
// Add an error with the key "test"
err := errortree.Add(nil, "test", errors.New("test error"))
// Call Set on the key, which will override it
err = errortree.Set(err, "test", errors.New("key re-used"))
fmt.Println(err.Error())
// Output: 1 error occurred:
//
// * test: key re-used
}
func ExampleFlatten() {
tree := &errortree.Tree{
Errors: map[string]error{
"a": errors.New("top-level"),
"b": &errortree.Tree{
Errors: map[string]error{
"c": errors.New("nested"),
},
},
},
}
flattened := errortree.Flatten(tree)
// Sort keys alphabetically so we get reproducible output
keys := errortree.Keys(tree)
for _, key := range keys {
fmt.Println("key: " + key + ", value: " + flattened[key].Error())
}
// Output: key: a, value: top-level
// key: b:c, value: nested
}
func ExampleKeys() {
tree := &errortree.Tree{
Errors: map[string]error{
"a": errors.New("top-level"),
"test": errors.New("test"),
"b": &errortree.Tree{
Errors: map[string]error{
"c": errors.New("nested"),
},
},
},
}
fmt.Println(strings.Join(errortree.Keys(tree), ", "))
// Output: a, b:c, test
}
func ExampleGet() {
tree := &errortree.Tree{
Errors: map[string]error{
"a": errors.New("top-level"),
"test": errors.New("test"),
"b": &errortree.Tree{
Errors: map[string]error{
"c": errors.New("nested"),
},
},
},
}
// Get can be used to retrieve an error by its key
fmt.Println(errortree.Get(tree, "a"))
// Nested retrieval is supported as well
fmt.Println(errortree.Get(tree, "b", "c"))
// Output: top-level
// nested
}
func ExampleGet_nested() {
tree := &errortree.Tree{
Errors: map[string]error{
"a": errors.New("top-level"),
"test": errors.New("test"),
"b": &errortree.Tree{
Errors: map[string]error{
"c": errors.New("nested"),
},
},
},
}
// Get tries to resolve the path exactly and returns nil if
// the path does not exist
fmt.Println(errortree.Get(tree, "b", "non-existent"))
// Output: <nil>
}
func ExampleGet_non_tree() {
// Get returns nil if the passed error is not a tree
fmt.Println(errortree.Get(errors.New("test"), "key"))
// Output: <nil>
}
func ExampleGetAny_non_tree() {
// GetAny always returns the error it got passed even if it is not a tree
fmt.Println(errortree.GetAny(errors.New("test"), "key"))
// Output: test
}
func ExampleGetAny_nested() {
// When GetAny does not encounter an exact match in the tree, it returns the most-specific match
tree := &errortree.Tree{
Errors: map[string]error{
"a": errors.New("top-level"),
"test": errors.New("test"),
"b": &errortree.Tree{
Errors: map[string]error{
"c": errors.New("nested"),
},
},
},
}
// Get tries to resolve the path exactly and returns nil if
// the path does not exist
fmt.Println(errortree.GetAny(tree, "b", "non-existent"))
// Output: 1 error occurred:
//
// * c: nested
}