diff --git a/src/HttpSender.php b/src/HttpSender.php index 2b59a85..4126479 100644 --- a/src/HttpSender.php +++ b/src/HttpSender.php @@ -129,9 +129,10 @@ function (HttpRequestException|TransferException $exception) use ($pendingReques $response = $this->createResponse($exception->response->toPsrResponse(), $pendingRequest, $psrRequest, $exception); - // Throw the exception our way + // Throw the exception our way if there is an exception. + // Otherwise we'll return the response. - throw $response->toException(); + return ($exception = $response->toException()) ? throw $exception : $response; } ); } diff --git a/tests/Feature/PoolTest.php b/tests/Feature/PoolTest.php index 67de824..7b69423 100644 --- a/tests/Feature/PoolTest.php +++ b/tests/Feature/PoolTest.php @@ -11,6 +11,7 @@ use Saloon\HttpSender\Tests\Fixtures\Requests\UserRequest; use Saloon\HttpSender\Tests\Fixtures\Connectors\HttpSenderConnector; use Saloon\HttpSender\Tests\Fixtures\Connectors\InvalidConnectionConnector; +use Saloon\HttpSender\Tests\Fixtures\Requests\ErrorRequestThatShouldBeTreatedAsSuccessful; test('you can create a pool on a connector', function () { $connector = new HttpSenderConnector; @@ -76,3 +77,26 @@ expect($count)->toEqual(5); }); + +test('if a pool has a failed response that should be treated as a successful response', function () { + $count = 0; + + $pool = (new HttpSenderConnector)->pool([ + new ErrorRequestThatShouldBeTreatedAsSuccessful, + ]); + + $pool->withResponseHandler(function (Response $response) use (&$count) { + expect($response)->toBeInstanceOf(Response::class); + expect($response->status())->toBe(404); + + $count++; + }); + + $promise = $pool->send(); + + expect($promise)->toBeInstanceOf(PromiseInterface::class); + + $promise->wait(); + + expect($count)->toEqual(1); +}); diff --git a/tests/Fixtures/Requests/ErrorRequestThatShouldBeTreatedAsSuccessful.php b/tests/Fixtures/Requests/ErrorRequestThatShouldBeTreatedAsSuccessful.php new file mode 100644 index 0000000..74d4161 --- /dev/null +++ b/tests/Fixtures/Requests/ErrorRequestThatShouldBeTreatedAsSuccessful.php @@ -0,0 +1,30 @@ +status() !== 404; + } +}