Repository navigation
Expand file tree
/
Copy patherrors_string_body_test.go
More file actions
55 lines (48 loc) · 2.67 KB
/
Copy patherrors_string_body_test.go
File metadata and controls
55 lines (48 loc) · 2.67 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
// Copyright (c) 2026 Matthew Winter
//
// This source code is licensed under the MIT license found in the LICENSE file
// in the root directory of this source tree.
package sigma_test
import (
"strings"
"testing"
"github.com/wintermi/sigma"
)
// Some providers, such as xAI and Ollama, send the error message as the
// "error" string; OAuth sends a code there. A message must reach
// ProviderMessage, where classification reads all of it, not only the first
// 2 KiB of the body preview.
func TestProviderErrorReadsStringErrorAsMessageOrCode(t *testing.T) {
t.Parallel()
// The overflow wording sits past the 2 KiB body preview.
longOverflow := "request rejected: " + strings.Repeat("x", 3000) + " prompt too long"
tests := []struct {
name string
body string
wantCode string
wantMessage string
}{
{name: "sentence", body: `{"error":"Incorrect API key provided: xai-***."}`, wantMessage: "Incorrect API key provided: xai-***."},
{name: "oauth code", body: `{"error":"invalid_grant","error_description":"Refresh token expired."}`, wantCode: "invalid_grant", wantMessage: "Refresh token expired."},
{name: "bare code", body: `{"error":"slow_down"}`, wantCode: "slow_down"},
{name: "status phrase beside message", body: `{"statusCode":400,"error":"Bad Request","message":"This model's maximum context length is 8192 tokens."}`, wantCode: "Bad Request", wantMessage: "This model's maximum context length is 8192 tokens."},
{name: "long message", body: `{"error":"` + longOverflow + `"}`, wantMessage: longOverflow},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
err := sigma.NewProviderError("custom", sigma.APIOpenAICompletions, "model", 400, "", 0, []byte(tt.body), sigma.ErrProviderResponse)
if err.ProviderCode != tt.wantCode || err.ProviderMessage != tt.wantMessage {
t.Fatalf("code = %.80q message = %.80q, want code %.80q message %.80q", err.ProviderCode, err.ProviderMessage, tt.wantCode, tt.wantMessage)
}
})
}
err := sigma.NewProviderError("custom", sigma.APIOpenAICompletions, "model", 400, "", 0, []byte(`{"error":"`+longOverflow+`"}`), sigma.ErrProviderResponse)
if got := sigma.ClassifyError(err).Class; got != sigma.ErrorClassContextOverflow {
t.Fatalf("long string error class = %q, want context overflow", got)
}
err = sigma.NewProviderError("custom", sigma.APIOpenAICompletions, "model", 400, "", 0, []byte(`{"statusCode":400,"error":"Bad Request","message":"This model's maximum context length is 8192 tokens."}`), sigma.ErrProviderResponse)
if got := sigma.ClassifyError(err).Class; got != sigma.ErrorClassContextOverflow {
t.Fatalf("status phrase error class = %q, want context overflow", got)
}
}