Skip to content

Commit

Permalink
refactor guzzle error handling to satisfy phpstan with guzzle 7
Browse files Browse the repository at this point in the history
  • Loading branch information
willpower232 committed May 6, 2021
1 parent 5c80ba8 commit faa3436
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions src/Browserless.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,16 +240,24 @@ private function render(array $options)
'json' => $options,
]);
} catch (\GuzzleHttp\Exception\ClientException $e) {
$message = 'No response';

$response = $e->getResponse();
$body = $response ? $response->getBody() : '';
$json = $response ? json_decode($body) : '';
$message = $body;
if (json_last_error() === JSON_ERROR_NONE) {
$messages = [];
foreach ($json as $error) {
$messages[] = $error->message;

/**
* You could use $e->hasResponse() but that is not accurate enough for phpstan
*/
if ($response !== null) {
$message = $response->getBody();

$json = json_decode($message);
if (json_last_error() === JSON_ERROR_NONE) {
$messages = [];
foreach ($json as $error) {
$messages[] = $error->message;
}
$message = implode(', ', $messages);
}
$message = implode(', ', $messages);
}

throw new Browserless\APIException("Failed to render PDF: {$message}", $e->getCode(), $e);
Expand Down

0 comments on commit faa3436

Please sign in to comment.