Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor guzzle error handling to satisfy phpstan with guzzle 7 #13

Merged
merged 2 commits into from
May 6, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions src/Browserless.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,16 +240,25 @@ 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,
* as phpstan will be analysing against method signatures from guzzle 6 & 7
*/
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