@@ -234,7 +234,7 @@ func (c *APIClient) decode(v any, b []byte) error {
234
234
return fmt.Errorf(" failed to unmarshal one of in response body: %w" , err)
235
235
}
236
236
} else {
237
- return errors.New(" Unknown type with GetActualInstance but no unmarshalObj.UnmarshalJSON defined" )
237
+ return errors.New(" unknown type with GetActualInstance but no unmarshalObj.UnmarshalJSON defined" )
238
238
}
239
239
} else if err := json.Unmarshal(b, v); err != nil { // simple model
240
240
return fmt.Errorf(" failed to unmarshal response body: %w" , err)
@@ -243,6 +243,33 @@ func (c *APIClient) decode(v any, b []byte) error {
243
243
return nil
244
244
}
245
245
246
+ func (c *APIClient) decodeError(res *http.Response, body []byte) error {
247
+ apiErr := &APIError{
248
+ Message: string(body), // default to the full body if we cannot guess the type of the error.
249
+ Status: res.StatusCode,
250
+ }
251
+
252
+ if strings.Contains(res.Header.Get("Content-Type"), "application/json") {
253
+ var errBase ErrorBase
254
+
255
+ err := c.decode(&errBase, body)
256
+ if err != nil {
257
+ apiErr.Message = err.Error()
258
+
259
+ return apiErr
260
+ }
261
+ if errBase.Message != nil {
262
+ apiErr.Message = *errBase.Message
263
+ }
264
+
265
+ apiErr.AdditionalProperties = errBase.AdditionalProperties
266
+ } else if strings.Contains(res.Header.Get("Content-Type"), "text/html") {
267
+ apiErr.Message = http.StatusText(res.StatusCode)
268
+ }
269
+
270
+ return apiErr
271
+ }
272
+
246
273
// Prevent trying to import "fmt"
247
274
func reportError(format string, a ...any) error {
248
275
return fmt.Errorf(format, a...)
@@ -299,16 +326,58 @@ func setBody(body any, c compression.Compression) (*bytes.Buffer, error) {
299
326
}
300
327
301
328
if bodyBuf.Len() == 0 {
302
- return nil, errors.New(" Invalid body type, or empty body" )
329
+ return nil, errors.New(" invalid body type, or empty body" )
303
330
}
304
331
return bodyBuf, nil
305
332
}
306
333
307
334
type APIError struct {
308
335
Message string
309
336
Status int
337
+ AdditionalProperties map[string]any
310
338
}
311
339
312
340
func (e APIError) Error() string {
313
341
return fmt.Sprintf(" API error [%d] %s" , e.Status, e.Message)
314
342
}
343
+
344
+ func (o APIError) MarshalJSON() ([]byte, error) {
345
+ toSerialize := map[string]any{
346
+ " message" : o.Message,
347
+ }
348
+
349
+ for key, value := range o.AdditionalProperties {
350
+ toSerialize[key] = value
351
+ }
352
+
353
+ serialized, err := json.Marshal(toSerialize)
354
+ if err != nil {
355
+ return nil, fmt.Errorf(" failed to marshal APIError: %w" , err)
356
+ }
357
+
358
+ return serialized, nil
359
+ }
360
+
361
+ func (o *APIError) UnmarshalJSON(bytes []byte) error {
362
+ type _APIError APIError
363
+ apiErr := _APIError{}
364
+
365
+ err := json.Unmarshal(bytes, &apiErr)
366
+ if err != nil {
367
+ return fmt.Errorf(" failed to unmarshal APIError: %w" , err)
368
+ }
369
+
370
+ *o = APIError(apiErr)
371
+
372
+ additionalProperties := make(map[string]any)
373
+
374
+ err = json.Unmarshal(bytes, &additionalProperties)
375
+ if err != nil {
376
+ return fmt.Errorf(" failed to unmarshal additionalProperties in APIError: %w" , err)
377
+ }
378
+
379
+ delete(additionalProperties, "message")
380
+ o.AdditionalProperties = additionalProperties
381
+
382
+ return nil
383
+ }
0 commit comments