-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrors_test.go
246 lines (213 loc) · 6.53 KB
/
errors_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
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
package errutil
import (
"errors"
"fmt"
"reflect"
"runtime"
"strings"
"testing"
)
func TestWrapError(t *testing.T) {
t.Run("happy path", func(t *testing.T) {
err := errors.New("yo son")
t.Run("shared", func(t *testing.T) {
for idx, tt := range sharedHappyPathTests {
t.Run(fmt.Sprintf("%d", idx), func(t *testing.T) {
e := Wrap(err, tt.opts...)
if len(tt.fns) != len(tt.expectations) {
t.Fatal("fns and expectations should be the same length")
}
for idx, fn := range tt.fns {
if tt.expectations[idx] != fn(e) {
t.Fatalf("expected %s to return %v", getFunctionName(fn), tt.expectations[idx])
}
}
})
}
})
t.Run("with internal error message", func(t *testing.T) {
e := Wrap(err, WithInternalErrorMessage("yolo"))
if !IsInternalErrorMessage(e) {
t.Fatal("expected true")
}
if GetInternalErrorMessage(e) != "yolo" {
t.Fatal("expected yolo")
}
})
t.Run("with code", func(t *testing.T) {
e := Wrap(err, WithCode("yolo"))
if !IsCodeable(e) {
t.Fatal("expected true")
}
if GetCode(e) != "yolo" {
t.Fatal("expected yolo")
}
})
t.Run("with status code", func(t *testing.T) {
e := Wrap(err, WithStatusCode(23))
if !IsStatusCodeable(e) {
t.Fatal("expected true")
}
if GetStatusCode(e) != 23 {
t.Fatal("expected 23")
}
})
t.Run("with too many requests", func(t *testing.T) {
e := Wrap(err, WithTooManyRequests())
if !IsTooManyRequests(e) {
t.Fatal("expected true")
}
})
t.Run("with nil error", func(t *testing.T) {
err := Wrap(nil, WithNotFound())
if err != nil {
t.Fatal("expected nil")
}
})
})
t.Run("should handle tagged errors as expected", func(t *testing.T) {
e := Wrap(errors.New("hi"), WithTags(NewTag("what", "is"), NewTag("hello", "world")))
if !IsTaggable(e) {
t.Fatal("expected IsTaggable to return true")
}
if tags := GetTags(e); len(tags) != 2 {
t.Fatalf("expected 2 tags back but got %d", len(tags))
} else {
t.Log(tags)
}
t.Run("should handle nested tags as well", func(t *testing.T) {
nested := Wrap(e, WithEasyTags("yes", "sir", "will", "be"))
if !IsTaggable(nested) {
t.Fatal("expected IsTaggable to return true")
}
if tags := GetTags(nested); len(tags) != 4 {
t.Fatalf("expected 4 tags back but got %d", len(tags))
} else {
t.Log(tags)
}
})
})
t.Run("should generate stack trace when option provided", func(t *testing.T) {
err := Wrap(errors.New("hey there"), WithStackTrace(0))
fmt.Printf("%+v\n", err)
})
}
func TestNewError(t *testing.T) {
t.Run("happy path", func(t *testing.T) {
err := "yo son"
for idx, tt := range sharedHappyPathTests {
t.Run(fmt.Sprintf("%d", idx), func(t *testing.T) {
e := New(err, tt.opts...)
if len(tt.fns) != len(tt.expectations) {
t.Fatal("fns and expectations should be the same length")
}
for idx, fn := range tt.fns {
if tt.expectations[idx] != fn(e) {
t.Fatalf("expected %s to return %v", getFunctionName(fn), tt.expectations[idx])
}
}
})
}
})
t.Run("should handle tagged errors as expected", func(t *testing.T) {
e := New("hi", WithTags(NewTag("what", "is"), NewTag("hello", "world")))
if !IsTaggable(e) {
t.Fatal("expected IsTaggable to return true")
}
if tags := GetTags(e); len(tags) != 2 {
t.Fatalf("expected 2 tags back but got %d", len(tags))
} else {
t.Log(tags)
}
t.Run("should handle nested tags as well", func(t *testing.T) {
nested := Wrap(e, WithEasyTags("yes", "sir", "will", "be"))
if !IsTaggable(nested) {
t.Fatal("expected IsTaggable to return true")
}
if tags := GetTags(nested); len(tags) != 4 {
t.Fatalf("expected 4 tags back but got %d", len(tags))
} else {
t.Log(tags)
}
})
})
t.Run("should generate stack trace when option provided", func(t *testing.T) {
err := New("hey there", WithStackTrace(0))
fmt.Printf("%+v\n", err)
})
}
var sharedHappyPathTests = []struct {
opts []OptsFunc
fns []CheckerFn
expectations []bool
}{
{
opts: []OptsFunc{WithAccessDenied(true)},
fns: append([]CheckerFn{IsAccessDenied}, allFuncsExcept(IsAccessDenied)...),
expectations: []bool{true, false, false, false, false, false, false, false},
},
{
opts: []OptsFunc{WithConflict(true)},
fns: append([]CheckerFn{IsConflict}, allFuncsExcept(IsConflict)...),
expectations: []bool{true, false, false, false, false, false, false, false},
},
{
opts: []OptsFunc{WithExists(true)},
fns: append([]CheckerFn{IsExist}, allFuncsExcept(IsExist)...),
expectations: []bool{true, false, false, false, false, false, false, false},
},
{
opts: []OptsFunc{WithNotFound(true)},
fns: append([]CheckerFn{IsNotFound}, allFuncsExcept(IsNotFound)...),
expectations: []bool{true, false, false, false, false, false, false, false},
},
{
opts: []OptsFunc{WithRateLimit(true)},
fns: append([]CheckerFn{IsRateLimit}, allFuncsExcept(IsRateLimit)...),
expectations: []bool{true, false, false, false, false, false, false, false},
},
{
opts: []OptsFunc{WithTooLarge(true)},
fns: append([]CheckerFn{IsTooLarge}, allFuncsExcept(IsTooLarge)...),
expectations: []bool{true, false, false, false, false, false, false, false},
},
{
opts: []OptsFunc{WithAccessDenied(true), WithTooLarge(true),
WithConflict(true), WithNotFound(true), WithExists(true),
WithRateLimit(true), WithTooLarge(true), WithTemporary(true),
WithUnauthorized(true),
},
fns: allFuncs,
expectations: []bool{true, true, true, true, true, true, true, true},
},
{
opts: []OptsFunc{WithAccessDenied(true), WithTooLarge(true),
WithConflict(true), WithNotFound(true), WithExists(true),
WithRateLimit(true), WithTooLarge(false),
},
fns: allFuncs,
expectations: []bool{true, true, true, true, true, false, false, false},
},
}
var allFuncs = []CheckerFn{
IsAccessDenied, IsConflict, IsExist, IsNotFound,
IsRateLimit, IsTooLarge, IsTemporary, IsUnauthorized,
}
func allFuncsExcept(fns ...CheckerFn) []CheckerFn {
results := make([]CheckerFn, 0, len(allFuncs)-len(fns))
lookup := make(map[string]struct{}, len(fns))
for idx := range fns {
lookup[getFunctionName(fns[idx])] = struct{}{}
}
for _, fn := range allFuncs {
if _, found := lookup[getFunctionName(fn)]; found {
continue
}
results = append(results, fn)
}
return results
}
func getFunctionName(i interface{}) string {
strs := strings.Split(runtime.FuncForPC(reflect.ValueOf(i).Pointer()).Name(), ".")
return strs[len(strs)-1]
}