From 1e6db573514094b25297b874427f58db2387ab65 Mon Sep 17 00:00:00 2001 From: Claudio Dekker Date: Wed, 6 Nov 2024 16:54:01 +0100 Subject: [PATCH] Throw separate "Forbidden" (403) exceptions --- src/Exceptions/ForbiddenException.php | 10 ++++++++++ src/MakesHttpRequests.php | 6 ++++++ tests/ForgeSDKTest.php | 14 ++++++++++++++ 3 files changed, 30 insertions(+) create mode 100644 src/Exceptions/ForbiddenException.php diff --git a/src/Exceptions/ForbiddenException.php b/src/Exceptions/ForbiddenException.php new file mode 100644 index 0000000..27ead79 --- /dev/null +++ b/src/Exceptions/ForbiddenException.php @@ -0,0 +1,10 @@ +getBody(), true)); } + if ($response->getStatusCode() === 403) { + throw new ForbiddenException((string) $response->getBody()); + } + if ($response->getStatusCode() == 404) { throw new NotFoundException; } diff --git a/tests/ForgeSDKTest.php b/tests/ForgeSDKTest.php index b39fcd1..cb1a609 100644 --- a/tests/ForgeSDKTest.php +++ b/tests/ForgeSDKTest.php @@ -5,6 +5,7 @@ use GuzzleHttp\Client; use GuzzleHttp\Psr7\Response; use Laravel\Forge\Exceptions\FailedActionException; +use Laravel\Forge\Exceptions\ForbiddenException; use Laravel\Forge\Exceptions\NotFoundException; use Laravel\Forge\Exceptions\RateLimitExceededException; use Laravel\Forge\Exceptions\TimeoutException; @@ -76,6 +77,19 @@ public function test_handling_404_errors() $forge->recipes(); } + public function test_handling_forbidden_requests(): void + { + $this->expectException(ForbiddenException::class); + + $forge = new Forge('123', $http = Mockery::mock(Client::class)); + + $http->shouldReceive('request')->once()->with('GET', 'recipes', [])->andReturn( + new Response(403) + ); + + $forge->recipes(); + } + public function test_handling_failed_action_errors() { $forge = new Forge('123', $http = Mockery::mock(Client::class));