Skip to content

Commit

Permalink
add exception for payment required
Browse files Browse the repository at this point in the history
  • Loading branch information
JonPurvis committed Jul 28, 2024
1 parent 113332a commit 331c7d5
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
12 changes: 12 additions & 0 deletions src/Exceptions/Request/Statuses/PaymentRequiredException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Saloon\Exceptions\Request\Statuses;

use Saloon\Exceptions\Request\ClientException;

class PaymentRequiredException extends ClientException
{
//
}
2 changes: 2 additions & 0 deletions src/Helpers/RequestExceptionHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Saloon\Helpers;

use Saloon\Exceptions\Request\Statuses\PaymentRequiredException;
use Throwable;
use Saloon\Http\Response;
use Saloon\Exceptions\Request\ClientException;
Expand Down Expand Up @@ -32,6 +33,7 @@ public static function create(Response $response, Throwable $previous = null): R
$requestException = match (true) {
// Built-in exceptions
$status === 401 => UnauthorizedException::class,
$status === 402 => PaymentRequiredException::class,
$status === 403 => ForbiddenException::class,
$status === 404 => NotFoundException::class,
$status === 405 => MethodNotAllowedException::class,
Expand Down
6 changes: 4 additions & 2 deletions tests/Unit/RequestExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

declare(strict_types=1);

use Saloon\Exceptions\Request\Statuses\PaymentRequiredException;
use Saloon\Http\Faking\MockClient;
use Saloon\Helpers\StatusCodeHelper;
use Saloon\Http\Faking\MockResponse;
Expand Down Expand Up @@ -30,11 +31,12 @@
$exception = $response->toException();

$message = sprintf('%s (%s) Response: %s', StatusCodeHelper::getMessage($status), $status, $response->body());

expect($exception)->toBeInstanceOf($expectedException);
expect($exception->getMessage())->toEqual($message);
})->with([
[401, UnauthorizedException::class],
[402, PaymentRequiredException::class],
[403, ForbiddenException::class],
[404, NotFoundException::class],
[405, MethodNotAllowedException::class],
Expand All @@ -45,7 +47,7 @@
[503, ServiceUnavailableException::class],
[504, GatewayTimeoutException::class],
[418, ClientException::class],
[402, ClientException::class],
[411, ClientException::class],
]);

test('when the failed method is customised the response will return ok request exceptions', function (int $status, string $expectedException) {
Expand Down

0 comments on commit 331c7d5

Please sign in to comment.