forked from zendev-sh/goai
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors_test.go
More file actions
467 lines (432 loc) · 12.8 KB
/
errors_test.go
File metadata and controls
467 lines (432 loc) · 12.8 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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
package goai
import (
"errors"
"net/http"
"strings"
"testing"
)
func TestIsOverflow(t *testing.T) {
tests := []struct {
message string
want bool
}{
{"prompt is too long", true},
{"Input is too long for requested model", true},
{"Request exceeds the context window", true},
{"input token count of 500000 exceeds the maximum of 200000", true},
{"maximum prompt length is 128000", true},
{"reduce the length of the messages", true},
{"maximum context length is 32768 tokens", true},
{"exceeds the limit of 100000", true},
{"exceeds the available context size", true},
{"greater than the context length", true},
{"context window exceeds limit", true},
{"exceeded model token limit", true},
{"context_length_exceeded", true},
{"context length exceeded", true},
{"400 (no body)", true},
{"413 (no body)", true},
{"413 status code (no body)", true},
// Negative cases
{"normal error message", false},
{"rate limit exceeded", false},
{"unauthorized", false},
{"", false},
}
for _, tt := range tests {
t.Run(tt.message, func(t *testing.T) {
got := IsOverflow(tt.message)
if got != tt.want {
t.Errorf("IsOverflow(%q) = %v, want %v", tt.message, got, tt.want)
}
})
}
}
func TestParseHTTPError_Overflow(t *testing.T) {
body := []byte(`{"error":{"message":"prompt is too long","type":"invalid_request_error"}}`)
err := ParseHTTPError("anthropic", 400, body)
var overflow *ContextOverflowError
if !errors.As(err, &overflow) {
t.Fatalf("expected ContextOverflowError, got %T: %v", err, err)
}
if overflow.Message != "prompt is too long" {
t.Errorf("expected 'prompt is too long', got %q", overflow.Message)
}
}
func TestParseHTTPError_APIError(t *testing.T) {
body := []byte(`{"error":{"message":"rate limit exceeded"}}`)
err := ParseHTTPError("openai", 429, body)
var apiErr *APIError
if !errors.As(err, &apiErr) {
t.Fatalf("expected APIError, got %T: %v", err, err)
}
if !apiErr.IsRetryable {
t.Error("expected 429 to be retryable")
}
}
func TestParseHTTPError_OpenAI404(t *testing.T) {
body := []byte(`{"error":{"message":"model not found"}}`)
err := ParseHTTPError("openai", 404, body)
var apiErr *APIError
if !errors.As(err, &apiErr) {
t.Fatalf("expected APIError, got %T", err)
}
if !apiErr.IsRetryable {
t.Error("expected OpenAI 404 to be retryable")
}
}
func TestParseHTTPError_NonOpenAI404(t *testing.T) {
body := []byte(`{"error":{"message":"not found"}}`)
err := ParseHTTPError("anthropic", 404, body)
var apiErr *APIError
if !errors.As(err, &apiErr) {
t.Fatalf("expected APIError, got %T", err)
}
if apiErr.IsRetryable {
t.Error("expected non-OpenAI 404 to not be retryable")
}
}
func TestParseHTTPError_EmptyBody(t *testing.T) {
err := ParseHTTPError("anthropic", 400, nil)
var overflow *ContextOverflowError
if !errors.As(err, &overflow) {
t.Fatalf("expected ContextOverflowError for '400 (no body)', got %T: %v", err, err)
}
}
func TestParseHTTPError_500(t *testing.T) {
body := []byte(`{"error":"internal server error"}`)
err := ParseHTTPError("anthropic", 500, body)
var apiErr *APIError
if !errors.As(err, &apiErr) {
t.Fatalf("expected APIError, got %T", err)
}
if !apiErr.IsRetryable {
t.Error("expected 500 to be retryable")
}
if apiErr.StatusCode != 500 {
t.Errorf("StatusCode = %d, want 500", apiErr.StatusCode)
}
}
func TestParseStreamError(t *testing.T) {
tests := []struct {
name string
body string
want *ParsedStreamError
}{
{
"context overflow",
`{"type":"error","error":{"code":"context_length_exceeded","message":"too long"}}`,
&ParsedStreamError{Type: "context_overflow", Message: "Input exceeds context window of this model"},
},
{
"insufficient quota",
`{"type":"error","error":{"code":"insufficient_quota","message":"quota exceeded"}}`,
&ParsedStreamError{Type: "api_error", Message: "Quota exceeded. Check your plan and billing details."},
},
{
"usage not included",
`{"type":"error","error":{"code":"usage_not_included","message":"upgrade"}}`,
&ParsedStreamError{Type: "api_error", Message: "To use Codex with your ChatGPT plan, upgrade to Plus."},
},
{
"invalid prompt with message",
`{"type":"error","error":{"code":"invalid_prompt","message":"bad input"}}`,
&ParsedStreamError{Type: "api_error", Message: "bad input"},
},
{
"invalid prompt without message",
`{"type":"error","error":{"code":"invalid_prompt","message":""}}`,
&ParsedStreamError{Type: "api_error", Message: "Invalid prompt."},
},
{
"not an error",
`{"type":"message_start"}`,
nil,
},
{
"invalid json",
`not json`,
nil,
},
{
"unknown error code",
`{"type":"error","error":{"code":"unknown_code","message":"something"}}`,
nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := ParseStreamError([]byte(tt.body))
if tt.want == nil {
if got != nil {
t.Errorf("expected nil, got %+v", got)
}
return
}
if got == nil {
t.Fatal("expected non-nil result")
}
if got.Type != tt.want.Type {
t.Errorf("type: got %q, want %q", got.Type, tt.want.Type)
}
if got.Message != tt.want.Message {
t.Errorf("message: got %q, want %q", got.Message, tt.want.Message)
}
})
}
}
func TestContextOverflowError_Error(t *testing.T) {
err := &ContextOverflowError{Message: "too long"}
if err.Error() != "too long" {
t.Errorf("Error() = %q, want %q", err.Error(), "too long")
}
}
func TestAPIError_Error(t *testing.T) {
err := &APIError{Message: "rate limited", StatusCode: 429}
if err.Error() != "rate limited" {
t.Errorf("Error() = %q, want %q", err.Error(), "rate limited")
}
}
func TestParseHTTPError_TopLevelMessage(t *testing.T) {
// Responses API format: top-level message field
body := []byte(`{"message":"model not available","code":"model_not_found"}`)
err := ParseHTTPError("openai", 404, body)
var apiErr *APIError
if !errors.As(err, &apiErr) {
t.Fatalf("expected APIError, got %T", err)
}
if apiErr.Message != "model not available" {
t.Errorf("Message = %q", apiErr.Message)
}
}
func TestParseHTTPError_ErrorAsString(t *testing.T) {
body := []byte(`{"error":"something went wrong"}`)
err := ParseHTTPError("anthropic", 500, body)
var apiErr *APIError
if !errors.As(err, &apiErr) {
t.Fatalf("expected APIError, got %T", err)
}
if apiErr.Message != "something went wrong" {
t.Errorf("Message = %q", apiErr.Message)
}
}
func TestParseHTTPError_InvalidJSON(t *testing.T) {
body := []byte(`not json at all`)
err := ParseHTTPError("openai", 502, body)
var apiErr *APIError
if !errors.As(err, &apiErr) {
t.Fatalf("expected APIError, got %T", err)
}
// Should fall back to status text + body for debugging
if apiErr.Message != "502 Bad Gateway: not json at all" {
t.Errorf("Message = %q, want '502 Bad Gateway: not json at all'", apiErr.Message)
}
}
func TestParseHTTPError_EmptyJSONObject(t *testing.T) {
body := []byte(`{}`)
err := ParseHTTPError("openai", 503, body)
var apiErr *APIError
if !errors.As(err, &apiErr) {
t.Fatalf("expected APIError, got %T", err)
}
// Empty JSON object falls back to status text + body for debugging
if apiErr.Message != "503 Service Unavailable: {}" {
t.Errorf("Message = %q", apiErr.Message)
}
}
func TestParseHTTPError_UnknownStatusCode(t *testing.T) {
// Status code 999 has no standard text
body := []byte(`not json`)
err := ParseHTTPError("openai", 999, body)
var apiErr *APIError
if !errors.As(err, &apiErr) {
t.Fatalf("expected APIError, got %T", err)
}
// Status code 999 has no standard text, but body is included
if apiErr.Message != "999 : not json" {
t.Errorf("Message = %q, want '999 : not json'", apiErr.Message)
}
}
func TestExtractErrorMessage_BodyTruncation(t *testing.T) {
// Body > 200 chars should be truncated with "..."
longBody := make([]byte, 300)
for i := range longBody {
longBody[i] = 'x'
}
err := ParseHTTPError("test", 502, longBody)
var apiErr *APIError
if !errors.As(err, &apiErr) {
t.Fatalf("expected APIError, got %T", err)
}
// Should end with "..."
if len(apiErr.Message) > 220 && !strings.HasSuffix(apiErr.Message, "...") {
t.Errorf("expected truncated body with ..., got len=%d", len(apiErr.Message))
}
if !strings.Contains(apiErr.Message, "...") {
t.Errorf("expected ... in truncated message, got %q", apiErr.Message)
}
}
func TestExtractErrorMessage_EmptyBodyWithStatusText(t *testing.T) {
// 999 with no body → "999 (no body)" which does not match overflow patterns.
err := ParseHTTPError("test", 999, nil)
var apiErr *APIError
if !errors.As(err, &apiErr) {
t.Fatalf("expected APIError, got %T: %v", err, err)
}
if apiErr.Message == "" {
t.Error("expected non-empty message for status 999 no body")
}
}
func TestExtractErrorMessage_StatusTextOnly(t *testing.T) {
// Status code 0 with empty body: body length == 0 triggers early return path
// "0 (no body)". Verify we get an error (not panic) and the message is non-empty.
err := ParseHTTPError("test", 0, []byte{})
if err == nil {
t.Fatal("expected error for status 0 empty body, got nil")
}
var apiErr *APIError
if !errors.As(err, &apiErr) {
// status 0 empty body → "0 (no body)"; if it doesn't match overflow it's an APIError
t.Fatalf("expected APIError, got %T: %v", err, err)
}
if apiErr.Message == "" {
t.Error("expected non-empty message for status 0 empty body")
}
}
func TestClassifyStreamError(t *testing.T) {
tests := []struct {
name string
body string
wantNil bool
wantType string // "overflow" or "api"
wantMsg string
}{
{
name: "nil for invalid json",
body: "not json",
wantNil: true,
},
{
name: "nil for non-error type",
body: `{"type":"message_start"}`,
wantNil: true,
},
{
name: "context overflow",
body: `{"type":"error","error":{"code":"context_length_exceeded","message":"too long"}}`,
wantType: "overflow",
wantMsg: "Input exceeds context window of this model",
},
{
name: "api error - insufficient quota",
body: `{"type":"error","error":{"code":"insufficient_quota","message":"quota exceeded"}}`,
wantType: "api",
wantMsg: "Quota exceeded. Check your plan and billing details.",
},
{
name: "api error - usage not included",
body: `{"type":"error","error":{"code":"usage_not_included","message":"upgrade"}}`,
wantType: "api",
wantMsg: "To use Codex with your ChatGPT plan, upgrade to Plus.",
},
{
name: "nil for unknown code",
body: `{"type":"error","error":{"code":"unknown","message":"nope"}}`,
wantNil: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := ClassifyStreamError([]byte(tt.body))
if tt.wantNil {
if err != nil {
t.Fatalf("expected nil, got %v", err)
}
return
}
if err == nil {
t.Fatal("expected non-nil error")
}
switch tt.wantType {
case "overflow":
var overflow *ContextOverflowError
if !errors.As(err, &overflow) {
t.Fatalf("expected ContextOverflowError, got %T", err)
}
if overflow.Message != tt.wantMsg {
t.Errorf("Message = %q, want %q", overflow.Message, tt.wantMsg)
}
case "api":
var apiErr *APIError
if !errors.As(err, &apiErr) {
t.Fatalf("expected APIError, got %T", err)
}
if apiErr.Message != tt.wantMsg {
t.Errorf("Message = %q, want %q", apiErr.Message, tt.wantMsg)
}
}
})
}
}
func TestParseHTTPErrorWithHeaders_RetryAfter(t *testing.T) {
tests := []struct {
name string
headers map[string]string
wantHeader string
wantValue string
}{
{
name: "retry-after header",
headers: map[string]string{"Retry-After": "5"},
wantHeader: "retry-after",
wantValue: "5",
},
{
name: "retry-after-ms header",
headers: map[string]string{"Retry-After-Ms": "500"},
wantHeader: "retry-after-ms",
wantValue: "500",
},
{
name: "both headers",
headers: map[string]string{"Retry-After": "5", "Retry-After-Ms": "500"},
wantHeader: "retry-after",
wantValue: "5",
},
{
name: "no retry headers",
},
{
name: "nil headers",
headers: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var h http.Header
if tt.headers != nil {
h = make(http.Header)
for k, v := range tt.headers {
h.Set(k, v)
}
}
err := ParseHTTPErrorWithHeaders("test", 429, []byte(`{"error":"rate limited"}`), h)
var apiErr *APIError
if !errors.As(err, &apiErr) {
t.Fatalf("expected APIError, got %T", err)
}
if tt.wantHeader != "" {
if apiErr.ResponseHeaders == nil {
t.Fatal("expected ResponseHeaders to be non-nil")
}
if apiErr.ResponseHeaders[tt.wantHeader] != tt.wantValue {
t.Errorf("ResponseHeaders[%q] = %q, want %q", tt.wantHeader, apiErr.ResponseHeaders[tt.wantHeader], tt.wantValue)
}
} else if tt.headers == nil {
if apiErr.ResponseHeaders != nil {
t.Errorf("expected nil ResponseHeaders, got %v", apiErr.ResponseHeaders)
}
}
})
}
}