1
1
package openai_test
2
2
3
3
import (
4
+ "encoding/json"
5
+
4
6
. "github.com/sashabaranov/go-openai"
5
7
"github.com/sashabaranov/go-openai/internal/test/checks"
6
8
@@ -110,7 +112,7 @@ func TestAPIError(t *testing.T) {
110
112
c := NewClient (apiToken + "_invalid" )
111
113
ctx := context .Background ()
112
114
_ , err = c .ListEngines (ctx )
113
- checks .NoError (t , err , "ListEngines did not fail" )
115
+ checks .HasError (t , err , "ListEngines should fail with an invalid key " )
114
116
115
117
var apiErr * APIError
116
118
if ! errors .As (err , & apiErr ) {
@@ -120,14 +122,108 @@ func TestAPIError(t *testing.T) {
120
122
if apiErr .StatusCode != 401 {
121
123
t .Fatalf ("Unexpected API error status code: %d" , apiErr .StatusCode )
122
124
}
123
- if * apiErr .Code != "invalid_api_key" {
124
- t .Fatalf ("Unexpected API error code: %s" , * apiErr .Code )
125
+
126
+ switch v := apiErr .Code .(type ) {
127
+ case string :
128
+ if v != "invalid_api_key" {
129
+ t .Fatalf ("Unexpected API error code: %s" , v )
130
+ }
131
+ default :
132
+ t .Fatalf ("Unexpected API error code type: %T" , v )
125
133
}
134
+
126
135
if apiErr .Error () == "" {
127
136
t .Fatal ("Empty error message occurred" )
128
137
}
129
138
}
130
139
140
+ func TestAPIErrorUnmarshalJSONInteger (t * testing.T ) {
141
+ var apiErr APIError
142
+ response := `{"code":418,"message":"I'm a teapot","param":"prompt","type":"teapot_error"}`
143
+ err := json .Unmarshal ([]byte (response ), & apiErr )
144
+ checks .NoError (t , err , "Unexpected Unmarshal API response error" )
145
+
146
+ switch v := apiErr .Code .(type ) {
147
+ case int :
148
+ if v != 418 {
149
+ t .Fatalf ("Unexpected API code integer: %d; expected 418" , v )
150
+ }
151
+ default :
152
+ t .Fatalf ("Unexpected API error code type: %T" , v )
153
+ }
154
+ }
155
+
156
+ func TestAPIErrorUnmarshalJSONString (t * testing.T ) {
157
+ var apiErr APIError
158
+ response := `{"code":"teapot","message":"I'm a teapot","param":"prompt","type":"teapot_error"}`
159
+ err := json .Unmarshal ([]byte (response ), & apiErr )
160
+ checks .NoError (t , err , "Unexpected Unmarshal API response error" )
161
+
162
+ switch v := apiErr .Code .(type ) {
163
+ case string :
164
+ if v != "teapot" {
165
+ t .Fatalf ("Unexpected API code string: %s; expected `teapot`" , v )
166
+ }
167
+ default :
168
+ t .Fatalf ("Unexpected API error code type: %T" , v )
169
+ }
170
+ }
171
+
172
+ func TestAPIErrorUnmarshalJSONNoCode (t * testing.T ) {
173
+ // test integer code
174
+ response := `{"message":"I'm a teapot","param":"prompt","type":"teapot_error"}`
175
+ var apiErr APIError
176
+ err := json .Unmarshal ([]byte (response ), & apiErr )
177
+ checks .NoError (t , err , "Unexpected Unmarshal API response error" )
178
+
179
+ switch v := apiErr .Code .(type ) {
180
+ case nil :
181
+ default :
182
+ t .Fatalf ("Unexpected API error code type: %T" , v )
183
+ }
184
+ }
185
+
186
+ func TestAPIErrorUnmarshalInvalidData (t * testing.T ) {
187
+ apiErr := APIError {}
188
+ data := []byte (`--- {"code":418,"message":"I'm a teapot","param":"prompt","type":"teapot_error"}` )
189
+ err := apiErr .UnmarshalJSON (data )
190
+ checks .HasError (t , err , "Expected error when unmarshaling invalid data" )
191
+
192
+ if apiErr .Code != nil {
193
+ t .Fatalf ("Expected nil code, got %q" , apiErr .Code )
194
+ }
195
+ if apiErr .Message != "" {
196
+ t .Fatalf ("Expected empty message, got %q" , apiErr .Message )
197
+ }
198
+ if apiErr .Param != nil {
199
+ t .Fatalf ("Expected nil param, got %q" , * apiErr .Param )
200
+ }
201
+ if apiErr .Type != "" {
202
+ t .Fatalf ("Expected empty type, got %q" , apiErr .Type )
203
+ }
204
+ }
205
+
206
+ func TestAPIErrorUnmarshalJSONInvalidParam (t * testing.T ) {
207
+ var apiErr APIError
208
+ response := `{"code":418,"message":"I'm a teapot","param":true,"type":"teapot_error"}`
209
+ err := json .Unmarshal ([]byte (response ), & apiErr )
210
+ checks .HasError (t , err , "Param should be a string" )
211
+ }
212
+
213
+ func TestAPIErrorUnmarshalJSONInvalidType (t * testing.T ) {
214
+ var apiErr APIError
215
+ response := `{"code":418,"message":"I'm a teapot","param":"prompt","type":true}`
216
+ err := json .Unmarshal ([]byte (response ), & apiErr )
217
+ checks .HasError (t , err , "Type should be a string" )
218
+ }
219
+
220
+ func TestAPIErrorUnmarshalJSONInvalidMessage (t * testing.T ) {
221
+ var apiErr APIError
222
+ response := `{"code":418,"message":false,"param":"prompt","type":"teapot_error"}`
223
+ err := json .Unmarshal ([]byte (response ), & apiErr )
224
+ checks .HasError (t , err , "Message should be a string" )
225
+ }
226
+
131
227
func TestRequestError (t * testing.T ) {
132
228
var err error
133
229
0 commit comments