Skip to content

Commit

Permalink
Merge pull request #187 from laravel/add-forbidden-handling
Browse files Browse the repository at this point in the history
Throw separate "Forbidden" (403) exceptions
  • Loading branch information
jbrooksuk authored Nov 6, 2024
2 parents 4d59baa + 1e6db57 commit ec2c659
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/Exceptions/ForbiddenException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Laravel\Forge\Exceptions;

use Exception;

class ForbiddenException extends Exception
{
//
}
6 changes: 6 additions & 0 deletions src/MakesHttpRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Exception;
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;
Expand Down Expand Up @@ -91,6 +92,7 @@ protected function request($verb, $uri, array $payload = [])
*
* @throws \Exception
* @throws \Laravel\Forge\Exceptions\FailedActionException
* @throws \Laravel\Forge\Exceptions\ForbiddenException
* @throws \Laravel\Forge\Exceptions\NotFoundException
* @throws \Laravel\Forge\Exceptions\ValidationException
* @throws \Laravel\Forge\Exceptions\RateLimitExceededException
Expand All @@ -101,6 +103,10 @@ protected function handleRequestError(ResponseInterface $response)
throw new ValidationException(json_decode((string) $response->getBody(), true));
}

if ($response->getStatusCode() === 403) {
throw new ForbiddenException((string) $response->getBody());
}

if ($response->getStatusCode() == 404) {
throw new NotFoundException;
}
Expand Down
14 changes: 14 additions & 0 deletions tests/ForgeSDKTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
Expand Down

0 comments on commit ec2c659

Please sign in to comment.