Skip to content

Commit

Permalink
ClientExceptions always have a response.
Browse files Browse the repository at this point in the history
  • Loading branch information
westy92 committed Dec 29, 2022
1 parent ec11dde commit f749221
Showing 1 changed file with 12 additions and 16 deletions.
28 changes: 12 additions & 16 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,22 +134,18 @@ private function request(string $path, array $query, string $type): Model\Standa

return $result;
} catch (ClientException $e) {
if ($e->hasResponse()) {
/**
* @var mixed
*/
$json = json_decode($e->getResponse()->getBody()->__toString(), true);
/**
* @var ?string
*/
$error = is_array($json) && isset($json['error']) && is_string($json['error']) ? $json['error'] : null;
throw new \RuntimeException($error
?? $e->getResponse()->getReasonPhrase()
?: strval($e->getResponse()->getStatusCode())
);
} else {
throw new \RuntimeException('Unable to parse response.');
}
/**
* @var mixed
*/
$json = json_decode($e->getResponse()->getBody()->__toString(), true);
/**
* @var ?string
*/
$error = is_array($json) && isset($json['error']) && is_string($json['error']) ? $json['error'] : null;
throw new \RuntimeException($error
?? $e->getResponse()->getReasonPhrase()
?: strval($e->getResponse()->getStatusCode())
);
} catch (NotEncodableValueException) {
throw new \RuntimeException('Unable to parse response.');
}
Expand Down

0 comments on commit f749221

Please sign in to comment.