diff --git a/lib/Api/RaiAcceptAPIApi.php b/lib/Api/RaiAcceptAPIApi.php index 887e11b..ce18bf1 100644 --- a/lib/Api/RaiAcceptAPIApi.php +++ b/lib/Api/RaiAcceptAPIApi.php @@ -73,15 +73,20 @@ public function processRequest(Request $request, $target_structure, $error_struc 'response' => $response, ]; } catch (ApiException $e) { - switch ($e->getCode()) { - case 400: + $status = (int) $e->getCode(); + $body = $e->getResponseBody(); + // Refusals and client errors may use 400, 403, 422, etc. — same ErrorResponse JSON shape. + if ( $status >= 400 && $status < 500 && is_string( $body ) && $body !== '' ) { + try { $data = ObjectSerializer::deserialize( - $e->getResponseBody(), + $body, $error_structure, $e->getResponseHeaders() ); - $e->setResponseObject($data); - break; + $e->setResponseObject( $data ); + } catch ( \Throwable $t ) { + // Body may not be ErrorResponse JSON (e.g. HTML proxy error). + } } throw $e; }