-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors_test.go
More file actions
253 lines (222 loc) · 6.64 KB
/
Copy patherrors_test.go
File metadata and controls
253 lines (222 loc) · 6.64 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
package ramp_test
import (
"errors"
"testing"
ramp "github.com/iamkanishka/ramp-go"
)
func TestError_Error_WithHTTPStatus(t *testing.T) {
e := &ramp.Error{
Type: ramp.ErrorTypeNotFound,
Message: "user not found",
StatusCode: 404,
TraceID: "trace-abc",
}
got := e.Error()
for _, want := range []string{"not_found", "404", "user not found", "trace-abc"} {
if !contains(got, want) {
t.Errorf("Error() = %q, missing %q", got, want)
}
}
}
func TestError_Error_WithoutHTTPStatus(t *testing.T) {
e := &ramp.Error{Type: ramp.ErrorTypeNetwork, Message: "connection refused"}
got := e.Error()
if !contains(got, "network_error") || !contains(got, "connection refused") {
t.Errorf("Error() = %q, missing type or message", got)
}
}
func TestError_IsRetryable(t *testing.T) {
retryable := []ramp.ErrorType{
ramp.ErrorTypeRateLimit,
ramp.ErrorTypeServer,
ramp.ErrorTypeNetwork,
}
for _, et := range retryable {
e := &ramp.Error{Type: et}
if !e.IsRetryable() {
t.Errorf("IsRetryable(%q): got false, want true", et)
}
}
nonRetryable := []ramp.ErrorType{
ramp.ErrorTypeAuthentication,
ramp.ErrorTypeAuthorization,
ramp.ErrorTypeNotFound,
ramp.ErrorTypeValidation,
ramp.ErrorTypeTimeout,
ramp.ErrorTypeDeferred,
ramp.ErrorTypeUnknown,
}
for _, et := range nonRetryable {
e := &ramp.Error{Type: et}
if e.IsRetryable() {
t.Errorf("IsRetryable(%q): got true, want false", et)
}
}
}
func TestAPIError_Maps400ToValidation(t *testing.T) {
e := ramp.APIError(400, []byte(`{"message":"email is required"}`), "trace-1")
if e.Type != ramp.ErrorTypeValidation {
t.Errorf("type: got %q, want validation_error", e.Type)
}
if e.StatusCode != 400 {
t.Errorf("status: got %d, want 400", e.StatusCode)
}
if e.Message != "email is required" {
t.Errorf("message: got %q", e.Message)
}
}
func TestAPIError_Maps401ToAuthentication(t *testing.T) {
e := ramp.APIError(401, []byte(`{"message":"unauthorized"}`), "")
if e.Type != ramp.ErrorTypeAuthentication {
t.Errorf("type: got %q, want authentication_error", e.Type)
}
}
func TestAPIError_Maps403ToAuthorization(t *testing.T) {
e := ramp.APIError(403, []byte(`{"message":"forbidden"}`), "")
if e.Type != ramp.ErrorTypeAuthorization {
t.Errorf("type: got %q, want authorization_error", e.Type)
}
}
func TestAPIError_Maps404ToNotFound(t *testing.T) {
e := ramp.APIError(404, []byte(`{"message":"not found"}`), "")
if e.Type != ramp.ErrorTypeNotFound {
t.Errorf("type: got %q, want not_found", e.Type)
}
}
func TestAPIError_Maps429ToRateLimit(t *testing.T) {
e := ramp.APIError(429, []byte(`{"message":"too many requests","retry_after":30}`), "")
if e.Type != ramp.ErrorTypeRateLimit {
t.Errorf("type: got %q, want rate_limit_error", e.Type)
}
if e.RetryAfter != 30 {
t.Errorf("RetryAfter: got %d, want 30", e.RetryAfter)
}
}
func TestAPIError_Maps429_DefaultRetryAfter60(t *testing.T) {
e := ramp.APIError(429, []byte(`{"message":"too many requests"}`), "")
if e.RetryAfter != 60 {
t.Errorf("RetryAfter: got %d, want 60 (default)", e.RetryAfter)
}
}
func TestAPIError_Maps500ToServer(t *testing.T) {
e := ramp.APIError(500, []byte(`{"message":"internal server error"}`), "trace-xyz")
if e.Type != ramp.ErrorTypeServer {
t.Errorf("type: got %q, want server_error", e.Type)
}
if e.TraceID != "trace-xyz" {
t.Errorf("traceID: got %q, want trace-xyz", e.TraceID)
}
}
func TestAPIError_Maps502To503To504ToServer(t *testing.T) {
for _, code := range []int{502, 503, 504} {
e := ramp.APIError(code, []byte(`{}`), "")
if e.Type != ramp.ErrorTypeServer {
t.Errorf("code %d: type: got %q, want server_error", code, e.Type)
}
}
}
func TestAPIError_FallsBackToErrorField(t *testing.T) {
e := ramp.APIError(400, []byte(`{"error":"bad_request"}`), "")
if e.Message != "bad_request" {
t.Errorf("message: got %q, want bad_request", e.Message)
}
}
func TestAPIError_FallsBackToHTTPStatusText(t *testing.T) {
e := ramp.APIError(418, []byte(`{}`), "")
if e.Message == "" {
t.Error("message should not be empty")
}
}
func TestAPIError_AttachesBody(t *testing.T) {
body := []byte(`{"message":"error","extra":"data"}`)
e := ramp.APIError(400, body, "")
if string(e.Body) != string(body) {
t.Errorf("body: got %q, want %q", e.Body, body)
}
}
func TestIsNotFound_True(t *testing.T) {
err := &ramp.Error{Type: ramp.ErrorTypeNotFound, StatusCode: 404, Message: "not found"}
if !ramp.IsNotFound(err) {
t.Error("IsNotFound: got false, want true")
}
}
func TestIsNotFound_False(t *testing.T) {
err := &ramp.Error{Type: ramp.ErrorTypeServer, StatusCode: 500, Message: "server error"}
if ramp.IsNotFound(err) {
t.Error("IsNotFound: got true, want false")
}
}
func TestIsNotFound_NonRampError(t *testing.T) {
if ramp.IsNotFound(errors.New("generic error")) {
t.Error("IsNotFound: got true for non-ramp error")
}
}
func TestIsNotFound_Nil(t *testing.T) {
if ramp.IsNotFound(nil) {
t.Error("IsNotFound(nil): got true, want false")
}
}
func TestIsRateLimit(t *testing.T) {
err := &ramp.Error{Type: ramp.ErrorTypeRateLimit, RetryAfter: 60}
if !ramp.IsRateLimit(err) {
t.Error("IsRateLimit: got false, want true")
}
}
func TestIsAuth_Authentication(t *testing.T) {
err := &ramp.Error{Type: ramp.ErrorTypeAuthentication}
if !ramp.IsAuth(err) {
t.Error("IsAuth(authentication): got false")
}
}
func TestIsAuth_Authorization(t *testing.T) {
err := &ramp.Error{Type: ramp.ErrorTypeAuthorization}
if !ramp.IsAuth(err) {
t.Error("IsAuth(authorization): got false")
}
}
func TestIsValidation(t *testing.T) {
err := &ramp.Error{Type: ramp.ErrorTypeValidation}
if !ramp.IsValidation(err) {
t.Error("IsValidation: got false, want true")
}
}
func TestError_ImplementsErrorInterface(_ *testing.T) {
var err error = &ramp.Error{Type: ramp.ErrorTypeNotFound, Message: "test"}
_ = err.Error() // must not panic
}
func TestError_ErrorsAs(t *testing.T) {
orig := &ramp.Error{Type: ramp.ErrorTypeServer, Message: "boom", StatusCode: 500}
wrapped := errors.Join(errors.New("outer"), orig)
var re *ramp.Error
if !errors.As(wrapped, &re) {
t.Fatal("errors.As failed")
}
if re.StatusCode != 500 {
t.Errorf("StatusCode: got %d, want 500", re.StatusCode)
}
}
func TestPtr_ReturnsPointerToValue(t *testing.T) {
s := ramp.Ptr("hello")
if *s != "hello" {
t.Errorf("Ptr string: got %q", *s)
}
n := ramp.Ptr(42)
if *n != 42 {
t.Errorf("Ptr int: got %d", *n)
}
b := ramp.Ptr(true)
if !*b {
t.Error("Ptr bool: got false")
}
}
func contains(s, sub string) bool {
return len(s) >= len(sub) && (s == sub || len(s) > 0 && containsHelper(s, sub))
}
func containsHelper(s, sub string) bool {
for i := 0; i <= len(s)-len(sub); i++ {
if s[i:i+len(sub)] == sub {
return true
}
}
return false
}