@@ -251,21 +251,36 @@ func (c *Client) fullURL(suffix string, args ...any) string {
251
251
}
252
252
253
253
func (c * Client ) handleErrorResp (resp * http.Response ) error {
254
- var errRes ErrorResponse
255
- err := json .NewDecoder (resp .Body ).Decode (& errRes )
256
- if err != nil || errRes .Error == nil {
257
- reqErr := & RequestError {
254
+ var errResp ErrorResponse
255
+ var compErrResp CompatibleErrorResponse
256
+
257
+ bodyBytes , err := io .ReadAll (resp .Body )
258
+ if err != nil {
259
+ return & RequestError {
258
260
HTTPStatusCode : resp .StatusCode ,
259
261
Err : err ,
260
262
}
261
- if errRes .Error != nil {
262
- reqErr .Err = errRes .Error
263
- }
264
- return reqErr
265
263
}
266
264
267
- errRes .Error .HTTPStatusCode = resp .StatusCode
268
- return errRes .Error
265
+ // Decode into ErrorResponse
266
+ // First attempt to decode into ErrorResponse
267
+ err = json .Unmarshal (bodyBytes , & errResp )
268
+ if err == nil && errResp .Error != nil {
269
+ errResp .Error .HTTPStatusCode = resp .StatusCode
270
+ return errResp .Error
271
+ }
272
+
273
+ // If the first decode didn't work or resulted in an empty Error, try CompatibleErrorResponse
274
+ err = json .Unmarshal (bodyBytes , & compErrResp )
275
+ if err == nil && compErrResp .APIError != nil {
276
+ compErrResp .HTTPStatusCode = resp .StatusCode
277
+ return compErrResp .APIError
278
+ }
279
+
280
+ return & RequestError {
281
+ HTTPStatusCode : resp .StatusCode ,
282
+ Err : err ,
283
+ }
269
284
}
270
285
271
286
func containsSubstr (s []string , e string ) bool {
0 commit comments