Repository navigation
Expand file tree
/
Copy patherrors_test.go
More file actions
221 lines (207 loc) · 6.66 KB
/
Copy patherrors_test.go
File metadata and controls
221 lines (207 loc) · 6.66 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
package typesafe
import (
"context"
"errors"
"net/http"
"strings"
"testing"
"time"
)
func TestExtractMessage(t *testing.T) {
cases := []struct {
name string
body any
want string
}{
{"string body", "plain failure", "plain failure"},
{"error string", map[string]any{"error": "bad request"}, "bad request"},
{"error object", map[string]any{"error": map[string]any{"message": "invalid key"}}, "invalid key"},
{"message", map[string]any{"message": "not found"}, "not found"},
{"detail string", map[string]any{"detail": "denied"}, "denied"},
{"detail object", map[string]any{"detail": map[string]any{"message": "missing"}}, "missing"},
{
"validation list",
map[string]any{"detail": []any{
map[string]any{"loc": []any{"body", "state"}, "msg": "field required"},
map[string]any{"loc": []any{"body", "questions", "tone"}, "msg": "too short"},
}},
"state: field required; questions.tone: too short",
},
{"no message", map[string]any{"unrelated": true}, ""},
{"empty string", "", ""},
{"number", 42, ""},
}
for _, testCase := range cases {
t.Run(testCase.name, func(t *testing.T) {
if got := extractMessage(testCase.body); got != testCase.want {
t.Fatalf("extractMessage = %q, want %q", got, testCase.want)
}
})
}
}
func TestDescribeError(t *testing.T) {
if got := describeError(http.StatusBadRequest, nil); got != http.StatusText(http.StatusBadRequest) {
t.Errorf("empty body = %q", got)
}
if got := describeError(http.StatusBadRequest, []byte(`{"error":"boom"}`)); got != "boom" {
t.Errorf("json body = %q", got)
}
if got := describeError(http.StatusBadRequest, []byte("plain text")); got != "plain text" {
t.Errorf("text body = %q", got)
}
long := strings.Repeat("x", maxErrorBodyLength+50)
got := describeError(http.StatusBadRequest, []byte(long))
if len(got) != maxErrorBodyLength+len("…") {
t.Errorf("long body was not truncated: %d chars", len(got))
}
if !strings.HasSuffix(got, "…") {
t.Error("truncated body should end with an ellipsis")
}
}
func TestParseErrorBody(t *testing.T) {
if got := parseErrorBody(nil); got != nil {
t.Errorf("empty body = %#v", got)
}
if got := parseErrorBody([]byte(`{"a":1}`)); got == nil {
t.Error("json body should decode")
}
if got := parseErrorBody([]byte("text")); got != "text" {
t.Errorf("text body = %#v", got)
}
}
func TestAPIErrorString(t *testing.T) {
err := &APIError{
StatusCode: http.StatusTooManyRequests,
Message: "slow down",
Endpoint: "POST https://api.typesafe.ai/v1/systemone",
RequestID: "req_123",
}
got := err.Error()
for _, want := range []string{"429", "slow down", "POST https://api.typesafe.ai/v1/systemone", "req_123"} {
if !strings.Contains(got, want) {
t.Errorf("error %q should contain %q", got, want)
}
}
}
func TestAPIErrorRetryAfter(t *testing.T) {
err := &APIError{Headers: http.Header{"Retry-After": {"3"}}}
if got := err.RetryAfter(); got != 3*time.Second {
t.Errorf("RetryAfter = %s", got)
}
}
func TestErrorUnwrap(t *testing.T) {
base := errors.New("base")
wrapped := &Error{Message: "context", Err: base}
if !errors.Is(wrapped, base) {
t.Error("Error should unwrap to its cause")
}
if got := wrapped.Error(); got != "context: base" {
t.Errorf("Error() = %q", got)
}
if got := (&Error{Message: "plain"}).Error(); got != "plain" {
t.Errorf("Error() = %q", got)
}
if got := (&Error{Err: base}).Error(); got != "base" {
t.Errorf("Error() = %q", got)
}
}
func TestConnectionErrorString(t *testing.T) {
err := &APIConnectionError{Endpoint: "GET https://api.typesafe.ai/v1/models", Err: errors.New("connection refused")}
got := err.Error()
if !strings.Contains(got, "connection error") || !strings.Contains(got, "connection refused") {
t.Errorf("Error() = %q", got)
}
if !errors.Is(err, err.Err) {
t.Error("APIConnectionError should unwrap to its cause")
}
}
func TestTimeoutErrorString(t *testing.T) {
err := &APITimeoutError{Endpoint: "POST https://api.typesafe.ai/v1/systemone", Timeout: 10 * time.Second, Err: context.DeadlineExceeded}
got := err.Error()
if !strings.Contains(got, "10s") {
t.Errorf("Error() = %q should mention the timeout", got)
}
if !errors.Is(err, context.DeadlineExceeded) {
t.Error("APITimeoutError should unwrap to context.DeadlineExceeded")
}
}
func TestUserAbortErrorString(t *testing.T) {
err := &UserAbortError{Endpoint: "POST https://api.typesafe.ai/v1/systemone", Err: context.Canceled}
got := err.Error()
if !strings.Contains(got, "aborted") {
t.Errorf("Error() = %q", got)
}
if strings.Contains(got, "context canceled") {
t.Errorf("Error() = %q should not repeat the bare context error", got)
}
if !errors.Is(err, context.Canceled) {
t.Error("UserAbortError should unwrap to context.Canceled")
}
}
func TestResponseErrorString(t *testing.T) {
err := &APIResponseError{
StatusCode: http.StatusOK,
Endpoint: "GET https://api.typesafe.ai/v1/models",
Err: errors.New("missing models field"),
}
got := err.Error()
if !strings.Contains(got, "invalid response data") || !strings.Contains(got, "missing models field") {
t.Errorf("Error() = %q", got)
}
}
func TestLogLevelParsing(t *testing.T) {
cases := map[string]LogLevel{
"debug": LogLevelDebug,
"INFO": LogLevelInfo,
"warn": LogLevelWarn,
"warning": LogLevelWarn,
"error": LogLevelError,
"off": LogLevelOff,
"none": LogLevelOff,
}
// Values are trimmed before parsing.
cases[" warn "] = LogLevelWarn
for input, want := range cases {
got, err := ParseLogLevel(input)
if err != nil {
t.Fatalf("ParseLogLevel(%q): %v", input, err)
}
if got != want {
t.Errorf("ParseLogLevel(%q) = %v, want %v", input, got, want)
}
}
if _, err := ParseLogLevel("verbose"); err == nil {
t.Error("expected an error for an unknown level")
}
}
func TestRedactHeaders(t *testing.T) {
header := http.Header{
"Authorization": {"Bearer secret"},
"X-Api-Key": {"secret"},
"Cookie": {"session=secret"},
"X-Custom": {"visible"},
}
redacted := redactHeaders(header)
if redacted["Authorization"][0] != redactedValue {
t.Error("authorization should be redacted")
}
if redacted["X-Api-Key"][0] != redactedValue {
t.Error("api key should be redacted")
}
if redacted["Cookie"][0] != redactedValue {
t.Error("cookie should be redacted")
}
if redacted["X-Custom"][0] != "visible" {
t.Error("custom headers should not be redacted")
}
if header["Authorization"][0] != "Bearer secret" {
t.Error("redaction must not mutate the original header")
}
formatted := formatHeaders(header)
if strings.Contains(formatted, "secret") {
t.Errorf("formatHeaders leaked a secret: %q", formatted)
}
if !strings.Contains(formatted, "X-Custom=visible") {
t.Errorf("formatHeaders = %q", formatted)
}
}