Skip to content

Commit

Permalink
Merge pull request #4 from Shoplo/hotfix/fix-detailed-exception-seria…
Browse files Browse the repository at this point in the history
…lization
  • Loading branch information
AdrianAdamiec authored Dec 2, 2020
2 parents 875f753 + a761f7d commit 70663fa
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/Shoplo/ShipX/Exception/BaseException.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down
12 changes: 9 additions & 3 deletions src/Shoplo/ShipX/Exception/ExceptionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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';
}
}

0 comments on commit 70663fa

Please sign in to comment.