-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy patherrors_test.go
More file actions
245 lines (227 loc) · 6.54 KB
/
errors_test.go
File metadata and controls
245 lines (227 loc) · 6.54 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
package maxigo
import (
"errors"
"fmt"
"io"
"testing"
)
func TestErrorKindString(t *testing.T) {
tests := []struct {
kind ErrorKind
want string
}{
{ErrAPI, "api"},
{ErrNetwork, "network"},
{ErrTimeout, "timeout"},
{ErrDecode, "decode"},
{ErrFetch, "fetch"},
{ErrorKind(99), "unknown"},
}
for _, tt := range tests {
if got := tt.kind.String(); got != tt.want {
t.Errorf("ErrorKind(%d).String() = %q, want %q", tt.kind, got, tt.want)
}
}
}
func TestErrorError(t *testing.T) {
tests := []struct {
name string
err *Error
want string
}{
{
name: "api error with status code",
err: apiError("SendMessage", 403, "you don't have permission"),
want: "SendMessage: api error 403: you don't have permission",
},
{
name: "network error",
err: networkError("GetChat", fmt.Errorf("connection refused")),
want: "GetChat: network: connection refused",
},
{
name: "timeout error",
err: timeoutError("GetUpdates", fmt.Errorf("context deadline exceeded")),
want: "GetUpdates: timeout: context deadline exceeded",
},
{
name: "decode error",
err: decodeError("SendMessage", fmt.Errorf("unexpected EOF")),
want: "SendMessage: decode: unexpected EOF",
},
{
name: "error without message",
err: &Error{Kind: ErrNetwork, Op: "GetBot"},
want: "GetBot: network",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.err.Error(); got != tt.want {
t.Errorf("Error() = %q, want %q", got, tt.want)
}
})
}
}
func TestErrorUnwrap(t *testing.T) {
underlying := io.ErrUnexpectedEOF
err := networkError("GetChat", underlying)
if !errors.Is(err, underlying) {
t.Error("errors.Is should find the underlying error")
}
var target *Error
if !errors.As(err, &target) {
t.Fatal("errors.As should extract *Error")
}
if target.Kind != ErrNetwork {
t.Errorf("Kind = %v, want ErrNetwork", target.Kind)
}
if target.Op != "GetChat" {
t.Errorf("Op = %q, want %q", target.Op, "GetChat")
}
}
func TestErrorUnwrapNil(t *testing.T) {
err := apiError("SendMessage", 403, "forbidden")
if err.Unwrap() != nil {
t.Error("API error should have nil Unwrap()")
}
}
func TestErrorTimeout(t *testing.T) {
tests := []struct {
kind ErrorKind
want bool
}{
{ErrTimeout, true},
{ErrAPI, false},
{ErrNetwork, false},
{ErrDecode, false},
}
for _, tt := range tests {
err := &Error{Kind: tt.kind, Op: "test"}
if got := err.Timeout(); got != tt.want {
t.Errorf("Error{Kind: %v}.Timeout() = %v, want %v", tt.kind, got, tt.want)
}
}
}
func TestErrorWrappedInFmtErrorf(t *testing.T) {
original := apiError("SendMessage", 429, "rate limited")
wrapped := fmt.Errorf("handler failed: %w", original)
var target *Error
if !errors.As(wrapped, &target) {
t.Fatal("errors.As should extract *Error from wrapped error")
}
if target.StatusCode != 429 {
t.Errorf("StatusCode = %d, want 429", target.StatusCode)
}
}
func TestErrEmptyToken(t *testing.T) {
if ErrEmptyToken == nil {
t.Fatal("ErrEmptyToken should not be nil")
}
if ErrEmptyToken.Error() != "bot token is empty" {
t.Errorf("ErrEmptyToken.Error() = %q, want %q", ErrEmptyToken.Error(), "bot token is empty")
}
}
func TestConstructors(t *testing.T) {
t.Run("apiError", func(t *testing.T) {
err := apiError("SendMessage", 500, "internal server error")
if err.Kind != ErrAPI {
t.Errorf("Kind = %v, want ErrAPI", err.Kind)
}
if err.StatusCode != 500 {
t.Errorf("StatusCode = %d, want 500", err.StatusCode)
}
if err.Message != "internal server error" {
t.Errorf("Message = %q, want %q", err.Message, "internal server error")
}
if err.Op != "SendMessage" {
t.Errorf("Op = %q, want %q", err.Op, "SendMessage")
}
if err.Err != nil {
t.Error("Err should be nil for API errors")
}
})
t.Run("networkError", func(t *testing.T) {
underlying := io.EOF
err := networkError("GetChat", underlying)
if err.Kind != ErrNetwork {
t.Errorf("Kind = %v, want ErrNetwork", err.Kind)
}
if err.StatusCode != 0 {
t.Errorf("StatusCode = %d, want 0", err.StatusCode)
}
if err.Err != underlying {
t.Error("Err should be the underlying error")
}
})
t.Run("timeoutError", func(t *testing.T) {
underlying := fmt.Errorf("context deadline exceeded")
err := timeoutError("GetUpdates", underlying)
if err.Kind != ErrTimeout {
t.Errorf("Kind = %v, want ErrTimeout", err.Kind)
}
if !err.Timeout() {
t.Error("Timeout() should return true")
}
if err.Err != underlying {
t.Error("Err should be the underlying error")
}
})
t.Run("decodeError", func(t *testing.T) {
underlying := fmt.Errorf("invalid character")
err := decodeError("SendMessage", underlying)
if err.Kind != ErrDecode {
t.Errorf("Kind = %v, want ErrDecode", err.Kind)
}
if err.Err != underlying {
t.Error("Err should be the underlying error")
}
})
t.Run("fetchError", func(t *testing.T) {
err := fetchError("UploadPhotoFromURL", 404, "not found")
if err.Kind != ErrFetch {
t.Errorf("Kind = %v, want ErrFetch", err.Kind)
}
if err.StatusCode != 404 {
t.Errorf("StatusCode = %d, want 404", err.StatusCode)
}
if err.Op != "UploadPhotoFromURL" {
t.Errorf("Op = %q, want %q", err.Op, "UploadPhotoFromURL")
}
want := "UploadPhotoFromURL: fetch error 404: not found"
if got := err.Error(); got != want {
t.Errorf("Error() = %q, want %q", got, want)
}
})
}
func TestErrorImplementsErrorInterface(t *testing.T) {
var _ error = (*Error)(nil)
}
func TestIsRetryable(t *testing.T) {
tests := []struct {
name string
err error
want bool
}{
{"429 rate limit", apiError("op", 429, "too many requests"), true},
{"not.ready message", apiError("op", 400, "attachment not.ready"), true},
{"not.processed message", apiError("op", 400, "file not.processed yet"), true},
{"403 forbidden", apiError("op", 403, "forbidden"), false},
{"500 server error", apiError("op", 500, "internal"), false},
{"400 bad request", apiError("op", 400, "invalid body"), false},
{"network error", networkError("op", fmt.Errorf("conn refused")), false},
{"timeout error", timeoutError("op", fmt.Errorf("deadline")), false},
{"decode error", decodeError("op", fmt.Errorf("bad json")), false},
{"nil error", nil, false},
{"plain error", fmt.Errorf("something"), false},
{"wrapped 429", fmt.Errorf("wrapped: %w", apiError("op", 429, "rate")), true},
{"wrapped not.ready", fmt.Errorf("wrapped: %w", apiError("op", 400, "not.ready")), true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := isRetryable(tt.err); got != tt.want {
t.Errorf("isRetryable() = %v, want %v", got, tt.want)
}
})
}
}