-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors_test.go
More file actions
227 lines (201 loc) · 6.01 KB
/
errors_test.go
File metadata and controls
227 lines (201 loc) · 6.01 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
// Copyright 2025 Matthew Gall <me@matthewgall.dev>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"errors"
"net/http"
"strings"
"testing"
)
func TestAPIError(t *testing.T) {
tests := []struct {
name string
statusCode int
endpoint string
message string
err error
wantString string
retryable bool
}{
{
name: "retryable 429 error",
statusCode: http.StatusTooManyRequests,
endpoint: "/api/sessions",
message: "rate limited",
err: nil,
wantString: "API error (429) at /api/sessions: rate limited",
retryable: true,
},
{
name: "non-retryable 404 error",
statusCode: http.StatusNotFound,
endpoint: "/api/account",
message: "not found",
err: nil,
wantString: "API error (404) at /api/account: not found",
retryable: false,
},
{
name: "error with underlying cause",
statusCode: http.StatusInternalServerError,
endpoint: "/api/sessions",
message: "server error",
err: errors.New("connection timeout"),
wantString: "connection timeout",
retryable: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
apiErr := NewAPIError(tt.statusCode, tt.endpoint, tt.message, tt.err)
// Check error string contains expected parts
errStr := apiErr.Error()
if !strings.Contains(errStr, tt.wantString) {
t.Errorf("Error() = %q, want to contain %q", errStr, tt.wantString)
}
// Check retryable status
if apiErr.Retryable != tt.retryable {
t.Errorf("Retryable = %v, want %v", apiErr.Retryable, tt.retryable)
}
// Check unwrap
if tt.err != nil && apiErr.Unwrap() != tt.err {
t.Errorf("Unwrap() = %v, want %v", apiErr.Unwrap(), tt.err)
}
})
}
}
func TestAuthError(t *testing.T) {
t.Run("with error code", func(t *testing.T) {
authErr := &AuthError{
Code: "KT-CT-1139",
Message: "JWT token expired",
}
errStr := authErr.Error()
if !strings.Contains(errStr, "KT-CT-1139") {
t.Errorf("Error() = %q, want to contain error code", errStr)
}
if !strings.Contains(errStr, "JWT token expired") {
t.Errorf("Error() = %q, want to contain message", errStr)
}
})
t.Run("without error code", func(t *testing.T) {
authErr := &AuthError{
Message: "invalid credentials",
}
errStr := authErr.Error()
if !strings.Contains(errStr, "invalid credentials") {
t.Errorf("Error() = %q, want to contain message", errStr)
}
})
}
func TestCacheError(t *testing.T) {
underlyingErr := errors.New("file not found")
cacheErr := &CacheError{
CacheType: "saving_sessions",
Operation: "read",
Err: underlyingErr,
}
errStr := cacheErr.Error()
if !strings.Contains(errStr, "saving_sessions") {
t.Errorf("Error() = %q, want to contain cache type", errStr)
}
if !strings.Contains(errStr, "read") {
t.Errorf("Error() = %q, want to contain operation", errStr)
}
if !strings.Contains(errStr, "file not found") {
t.Errorf("Error() = %q, want to contain underlying error", errStr)
}
if cacheErr.Unwrap() != underlyingErr {
t.Errorf("Unwrap() = %v, want %v", cacheErr.Unwrap(), underlyingErr)
}
}
func TestValidationError(t *testing.T) {
t.Run("with value", func(t *testing.T) {
valErr := &ValidationError{
Field: "port",
Value: 99999,
Message: "port out of range",
}
errStr := valErr.Error()
if !strings.Contains(errStr, "port") {
t.Errorf("Error() = %q, want to contain field name", errStr)
}
if !strings.Contains(errStr, "99999") {
t.Errorf("Error() = %q, want to contain value", errStr)
}
})
t.Run("without value", func(t *testing.T) {
valErr := &ValidationError{
Field: "account_id",
Message: "required",
}
errStr := valErr.Error()
if !strings.Contains(errStr, "account_id") {
t.Errorf("Error() = %q, want to contain field name", errStr)
}
})
}
func TestSessionError(t *testing.T) {
underlyingErr := errors.New("API timeout")
sessErr := &SessionError{
SessionID: "sess-123",
Operation: "join",
Err: underlyingErr,
}
errStr := sessErr.Error()
if !strings.Contains(errStr, "sess-123") {
t.Errorf("Error() = %q, want to contain session ID", errStr)
}
if !strings.Contains(errStr, "join") {
t.Errorf("Error() = %q, want to contain operation", errStr)
}
if sessErr.Unwrap() != underlyingErr {
t.Errorf("Unwrap() = %v, want %v", sessErr.Unwrap(), underlyingErr)
}
}
func TestIsRetryableStatus(t *testing.T) {
tests := []struct {
statusCode int
retryable bool
}{
{http.StatusOK, false}, // 200
{http.StatusBadRequest, false}, // 400
{http.StatusUnauthorized, false}, // 401
{http.StatusNotFound, false}, // 404
{http.StatusTooManyRequests, true}, // 429
{http.StatusInternalServerError, true}, // 500
{http.StatusBadGateway, true}, // 502
{http.StatusServiceUnavailable, true}, // 503
{http.StatusGatewayTimeout, true}, // 504
}
for _, tt := range tests {
t.Run(http.StatusText(tt.statusCode), func(t *testing.T) {
got := isRetryableStatus(tt.statusCode)
if got != tt.retryable {
t.Errorf("isRetryableStatus(%d) = %v, want %v", tt.statusCode, got, tt.retryable)
}
})
}
}
func TestErrorsAsInterface(t *testing.T) {
// Test that errors can be checked with errors.As
apiErr := NewAPIError(429, "/api/test", "rate limited", nil)
var targetAPIErr *APIError
if !errors.As(apiErr, &targetAPIErr) {
t.Error("errors.As should work with APIError")
}
if targetAPIErr.StatusCode != 429 {
t.Errorf("StatusCode = %d, want 429", targetAPIErr.StatusCode)
}
}