@@ -234,7 +234,7 @@ func (c *APIClient) decode(v any, b []byte) error {
234234 return fmt.Errorf(" failed to unmarshal one of in response body: %w" , err)
235235 }
236236 } 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" )
238238 }
239239 } else if err := json.Unmarshal(b, v); err != nil { // simple model
240240 return fmt.Errorf(" failed to unmarshal response body: %w" , err)
@@ -243,6 +243,33 @@ func (c *APIClient) decode(v any, b []byte) error {
243243 return nil
244244}
245245
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+
246273// Prevent trying to import "fmt"
247274func reportError(format string, a ...any) error {
248275 return fmt.Errorf(format, a...)
@@ -299,16 +326,58 @@ func setBody(body any, c compression.Compression) (*bytes.Buffer, error) {
299326 }
300327
301328 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" )
303330 }
304331 return bodyBuf, nil
305332}
306333
307334type APIError struct {
308335 Message string
309336 Status int
337+ AdditionalProperties map[string]any
310338}
311339
312340func (e APIError) Error() string {
313341 return fmt.Sprintf(" API error [%d] %s" , e.Status, e.Message)
314342}
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