📖 1 minute read
Found dead code in an API client today. Classic mistake with Laravel’s HTTP client.
The code looked reasonable at first glance:
$response = Http::post($url, $data)->throw();
if ($response->failed()) {
throw $response->serverError()
? new ServerException()
: $response->toException();
}
Spot the problem? The throw() method already throws an exception if the request fails. That if ($response->failed()) block will never execute — it’s unreachable.
Laravel’s throw() is basically:
if ($this->failed()) {
throw $this->toException();
}
return $this;
So you either use throw() for automatic exception handling, OR check failed() manually. Not both.
The fix: if you need custom exception logic, don’t use throw():
$response = Http::post($url, $data);
if ($response->failed()) {
throw $response->serverError()
? new CustomServerException()
: new CustomClientException();
}
Small mistake, but good reminder to understand what framework helpers actually do under the hood.

Leave a Reply