Skip to content

Commit ec2c659

Browse files
authored
Merge pull request #187 from laravel/add-forbidden-handling
Throw separate "Forbidden" (403) exceptions
2 parents 4d59baa + 1e6db57 commit ec2c659

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

src/Exceptions/ForbiddenException.php

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Laravel\Forge\Exceptions;
4+
5+
use Exception;
6+
7+
class ForbiddenException extends Exception
8+
{
9+
//
10+
}

src/MakesHttpRequests.php

+6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Exception;
66
use Laravel\Forge\Exceptions\FailedActionException;
7+
use Laravel\Forge\Exceptions\ForbiddenException;
78
use Laravel\Forge\Exceptions\NotFoundException;
89
use Laravel\Forge\Exceptions\RateLimitExceededException;
910
use Laravel\Forge\Exceptions\TimeoutException;
@@ -91,6 +92,7 @@ protected function request($verb, $uri, array $payload = [])
9192
*
9293
* @throws \Exception
9394
* @throws \Laravel\Forge\Exceptions\FailedActionException
95+
* @throws \Laravel\Forge\Exceptions\ForbiddenException
9496
* @throws \Laravel\Forge\Exceptions\NotFoundException
9597
* @throws \Laravel\Forge\Exceptions\ValidationException
9698
* @throws \Laravel\Forge\Exceptions\RateLimitExceededException
@@ -101,6 +103,10 @@ protected function handleRequestError(ResponseInterface $response)
101103
throw new ValidationException(json_decode((string) $response->getBody(), true));
102104
}
103105

106+
if ($response->getStatusCode() === 403) {
107+
throw new ForbiddenException((string) $response->getBody());
108+
}
109+
104110
if ($response->getStatusCode() == 404) {
105111
throw new NotFoundException;
106112
}

tests/ForgeSDKTest.php

+14
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use GuzzleHttp\Client;
66
use GuzzleHttp\Psr7\Response;
77
use Laravel\Forge\Exceptions\FailedActionException;
8+
use Laravel\Forge\Exceptions\ForbiddenException;
89
use Laravel\Forge\Exceptions\NotFoundException;
910
use Laravel\Forge\Exceptions\RateLimitExceededException;
1011
use Laravel\Forge\Exceptions\TimeoutException;
@@ -76,6 +77,19 @@ public function test_handling_404_errors()
7677
$forge->recipes();
7778
}
7879

80+
public function test_handling_forbidden_requests(): void
81+
{
82+
$this->expectException(ForbiddenException::class);
83+
84+
$forge = new Forge('123', $http = Mockery::mock(Client::class));
85+
86+
$http->shouldReceive('request')->once()->with('GET', 'recipes', [])->andReturn(
87+
new Response(403)
88+
);
89+
90+
$forge->recipes();
91+
}
92+
7993
public function test_handling_failed_action_errors()
8094
{
8195
$forge = new Forge('123', $http = Mockery::mock(Client::class));

0 commit comments

Comments
 (0)