Skip to content

Commit

Permalink
Do not throw on 404 (#3)
Browse files Browse the repository at this point in the history
* πŸ› Only throw if response status code is not 404

404 should not be treated as an error

* βœ… Add test for throwing errors

* πŸ› Use status function instead of getStatusCode
  • Loading branch information
SimonJnsson authored Jan 18, 2024
1 parent b464841 commit 6326cf1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/LaravelEconomicServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ public function packageBooted()
->withResponseMiddleware(
fn (Response $response) => $logger->onResponse($response)
)
->throw();
->throwIf(function (\Illuminate\Http\Client\Response $response) {
return $response->status() !== 404;
});
});

EconomicApiService::setDriver(new HttpEconomicDriver());
Expand Down
24 changes: 24 additions & 0 deletions tests/Unit/DriverTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use Illuminate\Http\Client\Request;
use Illuminate\Http\Client\RequestException;
use Illuminate\Support\Facades\Http;
use Morningtrain\Economic\Resources\Customer;

Expand Down Expand Up @@ -33,3 +34,26 @@
];
});
});

it('does not throw on 404', function () {
Http::fake([
'https://restapi.e-conomic.com/customers/1' => Http::response([], 404),
]);

Customer::find(1);
})->throwsNoExceptions();

it('throws on error status code', function (int $statusCode) {
Http::fake([
'https://restapi.e-conomic.com/customers/1' => Http::response([], $statusCode),
]);

Customer::find(1);
})
->throws(RequestException::class)
->with([
400,
401,
403,
500,
]);

0 comments on commit 6326cf1

Please sign in to comment.