Skip to content

Commit

Permalink
Improve logs for exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
javier-villatoro committed Aug 6, 2024
1 parent 477ba15 commit 2da6bdc
Showing 1 changed file with 13 additions and 17 deletions.
30 changes: 13 additions & 17 deletions src/Http/Client/LeverClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -472,22 +472,18 @@ private function checkExpandOptions(array|string $expand): bool

private function handleException(Exception $e, string $method, string $endpoint, array $options = []): void
{
$logDetails = [
'package' => 'Bluelightco\LeverPhp',
'method' => $method,
'endpoint' => $endpoint,
'options' => json_encode($options),
'response' => null,
];

if ($e instanceof ClientException) {
$logDetails['response'] = $e->getResponse() ? $e->getResponse()->getBody()->getContents() : null;
}

Log::error("HTTP $method error: ".$e->getMessage(), $logDetails);

$type = $e instanceof ClientException ? 'ClientException' : 'Exception';

throw new LeverClientException("$type error executing HTTP $method. Please check the logs for more details.");
Log::error("HTTP $method error: ".$e->getMessage(), [
'message' => $e->getMessage(),
'package_context' => [
'package' => 'Bluelightco\LeverPhp',
'method' => $method,
'endpoint' => $endpoint,
'options' => json_encode($options),
'response' => ($e instanceof ClientException ? ($e->getResponse() ? $e->getResponse()->getBody()->getContents() : null) : null),
],
'exception' => $e,
]);

throw new LeverClientException("Error executing HTTP $method. Please check the logs for more details.");
}
}

0 comments on commit 2da6bdc

Please sign in to comment.