-
Notifications
You must be signed in to change notification settings - Fork 350
Expand file tree
/
Copy pathapierror_body_test.go
More file actions
212 lines (201 loc) · 7 KB
/
Copy pathapierror_body_test.go
File metadata and controls
212 lines (201 loc) · 7 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
// Handwritten test, not generated. See CONTRIBUTING.md for details.
package openai_test
import (
"context"
"errors"
"io"
"net/http"
"strings"
"testing"
"github.com/openai/openai-go/v3"
"github.com/openai/openai-go/v3/option"
"github.com/openai/openai-go/v3/shared"
)
func TestAPIErrorWithNonObjectErrorBody(t *testing.T) {
client := openai.NewClient(
option.WithAPIKey("My API Key"),
option.WithHTTPClient(&http.Client{
Transport: &closureTransport{
fn: func(req *http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: http.StatusBadRequest,
Header: http.Header{"Content-Type": []string{"application/json"}},
Body: io.NopCloser(strings.NewReader(`{"error": "you must provide a model parameter"}`)),
}, nil
},
},
}),
)
_, err := client.Chat.Completions.New(context.Background(), openai.ChatCompletionNewParams{
Messages: []openai.ChatCompletionMessageParamUnion{{
OfUser: &openai.ChatCompletionUserMessageParam{
Content: openai.ChatCompletionUserMessageParamContentUnion{
OfString: openai.String("Say this is a test"),
},
},
}},
Model: shared.ChatModelGPT4o,
})
if err == nil {
t.Fatal("Expected an API error")
}
var apiErr *openai.Error
if !errors.As(err, &apiErr) {
t.Fatalf("Expected error to be *openai.Error, got %T: %v", err, err)
}
if apiErr.StatusCode != http.StatusBadRequest {
t.Errorf("Expected status code %d, got %d", http.StatusBadRequest, apiErr.StatusCode)
}
if !strings.Contains(apiErr.Message, "you must provide a model parameter") {
t.Errorf("Expected message to contain the raw body, got %q", apiErr.Message)
}
if apiErr.RawJSON() != `{"error": "you must provide a model parameter"}` {
t.Errorf("Expected RawJSON to return the raw body, got %q", apiErr.RawJSON())
}
if !strings.Contains(err.Error(), "you must provide a model parameter") {
t.Errorf("Expected Error() to contain the raw body, got %q", err.Error())
}
if apiErr.Response == nil {
t.Error("Expected response to be populated")
}
}
func TestAPIErrorWithNullErrorBody(t *testing.T) {
client := openai.NewClient(
option.WithAPIKey("My API Key"),
option.WithHTTPClient(&http.Client{
Transport: &closureTransport{
fn: func(req *http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: http.StatusInternalServerError,
Header: http.Header{"Content-Type": []string{"application/json"}},
Body: io.NopCloser(strings.NewReader(`{"error": null}`)),
}, nil
},
},
}),
)
_, err := client.Chat.Completions.New(context.Background(), openai.ChatCompletionNewParams{
Messages: []openai.ChatCompletionMessageParamUnion{{
OfUser: &openai.ChatCompletionUserMessageParam{
Content: openai.ChatCompletionUserMessageParamContentUnion{
OfString: openai.String("Say this is a test"),
},
},
}},
Model: shared.ChatModelGPT4o,
})
if err == nil {
t.Fatal("Expected an API error")
}
var apiErr *openai.Error
if !errors.As(err, &apiErr) {
t.Fatalf("Expected error to be *openai.Error, got %T: %v", err, err)
}
if apiErr.StatusCode != http.StatusInternalServerError {
t.Errorf("Expected status code %d, got %d", http.StatusInternalServerError, apiErr.StatusCode)
}
if apiErr.Message != `{"error": null}` {
t.Errorf("Expected message to contain the raw body, got %q", apiErr.Message)
}
if apiErr.RawJSON() != `{"error": null}` {
t.Errorf("Expected RawJSON to return the raw body, got %q", apiErr.RawJSON())
}
if !strings.Contains(err.Error(), `{"error": null}`) {
t.Errorf("Expected Error() to contain the raw body, got %q", err.Error())
}
}
func TestAPIErrorWithNonJSONErrorBody(t *testing.T) {
client := openai.NewClient(
option.WithAPIKey("My API Key"),
option.WithHTTPClient(&http.Client{
Transport: &closureTransport{
fn: func(req *http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: http.StatusBadGateway,
Header: http.Header{"Content-Type": []string{"text/html"}},
Body: io.NopCloser(strings.NewReader("<html><body>Bad Gateway</body></html>")),
}, nil
},
},
}),
)
_, err := client.Chat.Completions.New(context.Background(), openai.ChatCompletionNewParams{
Messages: []openai.ChatCompletionMessageParamUnion{{
OfUser: &openai.ChatCompletionUserMessageParam{
Content: openai.ChatCompletionUserMessageParamContentUnion{
OfString: openai.String("Say this is a test"),
},
},
}},
Model: shared.ChatModelGPT4o,
})
if err == nil {
t.Fatal("Expected an API error")
}
var apiErr *openai.Error
if !errors.As(err, &apiErr) {
t.Fatalf("Expected error to be *openai.Error, got %T: %v", err, err)
}
if apiErr.StatusCode != http.StatusBadGateway {
t.Errorf("Expected status code %d, got %d", http.StatusBadGateway, apiErr.StatusCode)
}
if !strings.Contains(apiErr.Message, "Bad Gateway") {
t.Errorf("Expected message to contain the raw body, got %q", apiErr.Message)
}
if !strings.Contains(apiErr.RawJSON(), "Bad Gateway") {
t.Errorf("Expected RawJSON to contain the raw body, got %q", apiErr.RawJSON())
}
if !strings.Contains(err.Error(), "Bad Gateway") {
t.Errorf("Expected Error() to contain the raw body, got %q", err.Error())
}
}
func TestAPIErrorWithObjectErrorBody(t *testing.T) {
client := openai.NewClient(
option.WithAPIKey("My API Key"),
option.WithHTTPClient(&http.Client{
Transport: &closureTransport{
fn: func(req *http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: http.StatusTooManyRequests,
Header: http.Header{"Content-Type": []string{"application/json"}},
Body: io.NopCloser(strings.NewReader(
`{"error": {"message": "rate limit exceeded", "type": "rate_limit_error", "param": null, "code": "rate_limit_exceeded"}}`,
)),
}, nil
},
},
}),
)
_, err := client.Chat.Completions.New(context.Background(), openai.ChatCompletionNewParams{
Messages: []openai.ChatCompletionMessageParamUnion{{
OfUser: &openai.ChatCompletionUserMessageParam{
Content: openai.ChatCompletionUserMessageParamContentUnion{
OfString: openai.String("Say this is a test"),
},
},
}},
Model: shared.ChatModelGPT4o,
})
if err == nil {
t.Fatal("Expected an API error")
}
var apiErr *openai.Error
if !errors.As(err, &apiErr) {
t.Fatalf("Expected error to be *openai.Error, got %T: %v", err, err)
}
if apiErr.StatusCode != http.StatusTooManyRequests {
t.Errorf("Expected status code %d, got %d", http.StatusTooManyRequests, apiErr.StatusCode)
}
if apiErr.Message != "rate limit exceeded" {
t.Errorf("Expected message %q, got %q", "rate limit exceeded", apiErr.Message)
}
if apiErr.Type != "rate_limit_error" {
t.Errorf("Expected type %q, got %q", "rate_limit_error", apiErr.Type)
}
if !strings.Contains(apiErr.RawJSON(), "rate limit exceeded") {
t.Errorf("Expected RawJSON to contain the parsed error payload, got %q", apiErr.RawJSON())
}
if !strings.Contains(err.Error(), "rate limit exceeded") {
t.Errorf("Expected Error() to contain the parsed error payload, got %q", err.Error())
}
}