Skip to content

Commit

Permalink
Adding exceptions managment
Browse files Browse the repository at this point in the history
  • Loading branch information
javier-villatoro committed Aug 6, 2024
1 parent dcf13c5 commit fa9fa2b
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 5 deletions.
13 changes: 13 additions & 0 deletions src/Exceptions/LeverClientException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Bluelightco\LeverPhp\Exceptions;

use RuntimeException;

class LeverClientException extends RuntimeException
{
public function __construct($message, $code = 0, \Exception $previous = null)
{
parent::__construct("Bluelightco\LeverPhp: " . $message, $code, $previous);
}
}
38 changes: 35 additions & 3 deletions src/Http/Client/LeverClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
use Psr\Http\Message\StreamInterface;
use Spatie\GuzzleRateLimiterMiddleware\RateLimiterMiddleware;
use Spatie\GuzzleRateLimiterMiddleware\Store;
use Illuminate\Support\Facades\Log;
use Bluelightco\LeverPhp\Exceptions\LeverClientException;
use Exception;

class LeverClient
{
Expand Down Expand Up @@ -74,7 +77,9 @@ public function get(): ResponseInterface
try {
return $this->sendRequest('GET', $this->endpoint, $this->options);
} catch (ClientException $e) {
throw $e;
$this->handleException($e, __METHOD__);
} catch (Exception $e) {
$this->handleException($e, __METHOD__);
} finally {
$this->reset();
}
Expand All @@ -85,7 +90,9 @@ public function post(array $body = []): ResponseInterface
try {
return $this->sendRequest('POST', $this->endpoint, $this->prepareOptions($body));
} catch (ClientException $e) {
throw $e;
$this->handleException($e, __METHOD__, $body);
} catch (Exception $e) {
$this->handleException($e, __METHOD__, $body);
} finally {
$this->reset();
}
Expand All @@ -96,7 +103,9 @@ public function put(array $body = []): ResponseInterface
try {
return $this->sendRequest('PUT', $this->endpoint, $this->prepareOptions($body));
} catch (ClientException $e) {
throw $e;
$this->handleException($e, __METHOD__, $body);
} catch (Exception $e) {
$this->handleException($e, __METHOD__, $body);
} finally {
$this->reset();
}
Expand Down Expand Up @@ -476,4 +485,27 @@ private function checkExpandOptions(array|string $expand): bool

return true;
}

private function handleException(\Exception $e, string $method, array $body = []): void
{
$logDetails = [
'package' => 'Bluelightco\LeverPhp',
'method' => $method,
'endpoint' => $this->endpoint,
];

if (! empty($body)) {
$logDetails['body'] = json_encode($body);
}

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

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

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

throw new LeverClientException("$type error executing HTTP request in $method. Please check the logs for more details.");
}
}
4 changes: 2 additions & 2 deletions tests/OpportunitiesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace Bluelightco\LeverPhp\Tests;

use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Psr7\Response;
use Illuminate\Support\LazyCollection;
use Bluelightco\LeverPhp\Exceptions\LeverClientException;

class OpportunitiesTest extends TestCase
{
Expand Down Expand Up @@ -108,7 +108,7 @@ public function create_opportunity()
/** @test */
public function fail_to_create_opportunity_when_no_perform_as_parameter_included()
{
$this->expectException(ClientException::class);
$this->expectException(LeverClientException::class);

$this->mockHandler->append(new Response(400, [],
'{"code": "BadRequestError", "message": "Missing perform_as parameter. Please specify a user for which to perform this create."}'
Expand Down

0 comments on commit fa9fa2b

Please sign in to comment.