diff --git a/src/Exception/ExceptionFactory.php b/src/Exception/ExceptionFactory.php index d78b096..db4c93b 100644 --- a/src/Exception/ExceptionFactory.php +++ b/src/Exception/ExceptionFactory.php @@ -30,7 +30,7 @@ public static function create(int $status, string $body = ''): ?Exception case Response::HTTP_BAD_REQUEST: $body = json_decode($body, true, 512, JSON_THROW_ON_ERROR); - return new \RuntimeException("Error: $status; Body: " . $body['message']); + return new \RuntimeException("Error: $status; Body: " . self::getErrorMessage($body)); default: if ($status < 400) { return null; @@ -39,4 +39,24 @@ public static function create(int $status, string $body = ''): ?Exception return new \RuntimeException("Error: $status; Known body: $body"); } } + + private static function getErrorMessage(mixed $body): string + { + if (isset($body['message'])) { + return $body['message']; + } + + if ($body['error']) { + return json_encode( + [ + 'error' => $body['error'], + 'error_description' => $body['error_description'], + 'hint' => $body['hint'], + ], + JSON_THROW_ON_ERROR, + ); + } + + return 'unknown error'; + } }