From a761f7d08ceff83f3948d3282abfc42a8b383536 Mon Sep 17 00:00:00 2001 From: adrianadamiec Date: Tue, 1 Dec 2020 16:35:52 +0100 Subject: [PATCH] fix detailed exception serialization --- src/Shoplo/ShipX/Exception/BaseException.php | 3 ++- src/Shoplo/ShipX/Exception/ExceptionManager.php | 12 +++++++++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/Shoplo/ShipX/Exception/BaseException.php b/src/Shoplo/ShipX/Exception/BaseException.php index b2632a4..96c9ab8 100644 --- a/src/Shoplo/ShipX/Exception/BaseException.php +++ b/src/Shoplo/ShipX/Exception/BaseException.php @@ -21,11 +21,12 @@ public function __construct(\Throwable $previous, $body = null) $this->body = $body; if (null !== $body) { + $code = $body['status']; $msg = $body['message']; if (!empty($body['details'])) { - $body = $body['details']; + $msg .= $body['details']; } elseif (array_key_exists('error', $body)) { $body = [ $body['error'] => $body['message'], diff --git a/src/Shoplo/ShipX/Exception/ExceptionManager.php b/src/Shoplo/ShipX/Exception/ExceptionManager.php index 71f2b88..560de9c 100644 --- a/src/Shoplo/ShipX/Exception/ExceptionManager.php +++ b/src/Shoplo/ShipX/Exception/ExceptionManager.php @@ -17,11 +17,11 @@ public static function throwException(\Throwable $e): void } if (method_exists($e, 'getResponse')) { + if ($e->getResponse() instanceof ResponseInterface) { - $headers = $e->getResponse()->getHeaders(false); - $contentType = $headers['content-type'][0] ?? 'application/json'; - if (false !== strpos($contentType,'application/json')) { + if (false !== strpos(self::getResponseContentType($e),'application/json')) { + $body = $e->getResponse()->toArray(false); } } else { @@ -46,4 +46,10 @@ public static function throwException(\Throwable $e): void throw $e; } } + + private static function getResponseContentType(\Exception $e) + { + $headers = $e->getResponse()->getHeaders(false); + return $headers['content-type'][0] ?? 'application/json'; + } }