How do I get the HTTP Status Code from an Exception thrown in Laravel?
I am currently integrating some logic in App/Exceptions/Handler.php. I would like to be able to access the HTTP Status Code on the $exception variable:
public function report(Throwable $exception) < dd($exception->statusCode); parent::report($exception); >
Symfony\Component\HttpKernel\Exception\NotFoundHttpException >
3 Answers 3
If you look at the source code of Symfony\Component\HttpKernel\Exception\NotFoundHttpException you will find that it extends Symfony\Component\HttpKernel\Exception\HttpException if you look at the declaration of the class you will see that $statusCode is private but it has a getter method
class HttpException extends \RuntimeException implements HttpExceptionInterface < private $statusCode; private $headers; public function __construct(int $statusCode, string $message = null, \Throwable $previous = null, array $headers = [], ?int $code = 0) < $this->statusCode = $statusCode; $this->headers = $headers; parent::__construct($message, $code, $previous); > public function getStatusCode() < return $this->statusCode; > //. >
As such you simply need to do $exception->getStatusCode() to retrieve the status code (404 in your case) though you should do a check to make sure your throwable implements the HttpExceptionInterface because that might not always be the case and so the method would not exist and you would get a fatal error
if ($exception instanceof \Symfony\Component\HttpKernel\Exception\HttpExceptionInterface) < $code = $exception->getStatusCode(); >
Thank you so much. Can you think of any edge cases where a different kind of exception may be thrown, so that I could test this out please?
Any other exception thrown in your code like LogicException or a php error can be caught as a throwable and be reported, you can check it by throw new LogicException(») within your controller and see what you get, the NotFoundHttpException is likely only thrown because your route is not being matched but you can definitely have non HttpException in there
I’ve managed to get it worse in Laravel 8 but there’s no space between «Error» and «404», not ideal for showing to users.
follow @Tofandel answer, my practice is as follows,
Since this question is relatively new I presume you are suing Laravel version 7 or 8. According to the documentary of Error Handling , the correct page rendering method has now been as
public function render($request, Throwable $exception) < if ($exception instanceof CustomException) < return response()->view('errors.custom', [], 500); > return parent::render($request, $exception); >
In order to use customised error view, you would have to tell Laravel what kind of Exception instance your thrown $exception is. To overwrite the internal error handling you would want to catch the thrown as a HTTP exception. For these versions the correct Class is as Symfony\Component\HttpKernel\Exception\ . Therefore,
- bring in the handling Class by
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
on the top of your app\Exceptions\Handler.php - modify the default render() method to something like follows in accordance to your scenario,
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface; // // public function render($request, Throwable $exception) < // return parent::render($request, $exception); if ($exception instanceof HttpExceptionInterface) < if (env('APP_ENV') === 'production' && $exception->getStatusCode() == 404) < return response()->view('errors.404', [], 404); > if (env('APP_ENV') === 'production' && $exception->getStatusCode() == 500) < return response()->view('errors.500', [], 500); > > return parent::render($request, $exception); >
Getting 404 in Laravel on Heroku server
I am deploying my application from Gitlab using ci/cd, it’s successfully deployed. And it’s also working on my local system and shared server but not working on Heroku Also I also try to delete htaccess file but it is still not working.
< "message": "", "exception": "Symfony\\Component\\HttpKernel\\Exception\\NotFoundHttpException", "file": "/app/vendor/laravel/framework/src/Illuminate/Routing/AbstractRouteCollection.php", "line": 43, "trace": [ < "file": "/app/vendor/laravel/framework/src/Illuminate/Routing/RouteCollection.php", "line": 162, "function": "handleMatchedRoute", "class": "Illuminate\\Routing\\AbstractRouteCollection", "type": "->" >, < "file": "/app/vendor/laravel/framework/src/Illuminate/Routing/Router.php", "line": 639, "function": "match", "class": "Illuminate\\Routing\\RouteCollection", "type": "->" >, < "file": "/app/vendor/laravel/framework/src/Illuminate/Routing/Router.php", "line": 628, "function": "findRoute", "class": "Illuminate\\Routing\\Router", "type": "->" >, < "file": "/app/vendor/laravel/framework/src/Illuminate/Routing/Router.php", "line": 617, "function": "dispatchToRoute", "class": "Illuminate\\Routing\\Router", "type": "->" >, < "file": "/app/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php", "line": 165, "function": "dispatch", "class": "Illuminate\\Routing\\Router", "type": "->" >, < "file": "/app/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php", "line": 128, "function": "Illuminate\\Foundation\\Http\\", "class": "Illuminate\\Foundation\\Http\\Kernel", "type": "->" >, < "file": "/app/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php", "line": 21, "function": "Illuminate\\Pipeline\\", "class": "Illuminate\\Pipeline\\Pipeline", "type": "->" >, < "file": "/app/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php", "line": 167, "function": "handle", "class": "Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest", "type": "->" >, < "file": "/app/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php", "line": 21, "function": "Illuminate\\Pipeline\\", "class": "Illuminate\\Pipeline\\Pipeline", "type": "->" >, < "file": "/app/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php", "line": 167, "function": "handle", "class": "Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest", "type": "->" >, < "file": "/app/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ValidatePostSize.php", "line": 27, "function": "Illuminate\\Pipeline\\", "class": "Illuminate\\Pipeline\\Pipeline", "type": "->" >, < "file": "/app/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php", "line": 167, "function": "handle", "class": "Illuminate\\Foundation\\Http\\Middleware\\ValidatePostSize", "type": "->" >, < "file": "/app/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode.php", "line": 63, "function": "Illuminate\\Pipeline\\", "class": "Illuminate\\Pipeline\\Pipeline", "type": "->" >, < "file": "/app/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php", "line": 167, "function": "handle", "class": "Illuminate\\Foundation\\Http\\Middleware\\CheckForMaintenanceMode", "type": "->" >, < "file": "/app/vendor/fruitcake/laravel-cors/src/HandleCors.php", "line": 37, "function": "Illuminate\\Pipeline\\", "class": "Illuminate\\Pipeline\\Pipeline", "type": "->" >, < "file": "/app/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php", "line": 167, "function": "handle", "class": "Fruitcake\\Cors\\HandleCors", "type": "->" >, < "file": "/app/vendor/fideloper/proxy/src/TrustProxies.php", "line": 57, "function": "Illuminate\\Pipeline\\", "class": "Illuminate\\Pipeline\\Pipeline", "type": "->" >, < "file": "/app/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php", "line": 167, "function": "handle", "class": "Fideloper\\Proxy\\TrustProxies", "type": "->" >, < "file": "/app/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php", "line": 103, "function": "Illuminate\\Pipeline\\", "class": "Illuminate\\Pipeline\\Pipeline", "type": "->" >, < "file": "/app/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php", "line": 140, "function": "then", "class": "Illuminate\\Pipeline\\Pipeline", "type": "->" >, < "file": "/app/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php", "line": 109, "function": "sendRequestThroughRouter", "class": "Illuminate\\Foundation\\Http\\Kernel", "type": "->" >, < "file": "/app/public/index.php", "line": 55, "function": "handle", "class": "Illuminate\\Foundation\\Http\\Kernel", "type": "->" > ]
Laravel — Error when calling my logout route
I did the installation of laravel as sent by the basic authentication guide with auth, my login works and returns me a token, but when I try to log out it returns me the following error:
HTTP/1.0 404 Not Found Host: 127.0.0.1:8000 Date: Wed, 09 Dec 2020 19:33:08 GMT, Wed, 09 Dec 2020 19:33:08 GMT Connection: close X-Powered-By: PHP/7.2.24-0ubuntu0.18.04.7 Cache-Control: no-cache, private Content-Type: application/json Access-Control-Allow-Origin: * Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS Access-Control-Allow-Headers: X-Requested-With, Content-Type, X-Token-Auth, Authorization < "message": "", "exception": "Symfony\\Component\\HttpKernel\\Exception\\NotFoundHttpException", "file": "/home/shinier01/Projetos/Condivest/api-condinvest/api/vendor/laravel/framework/src/Illuminate/Routing/AbstractRouteCollection.php", "line": 43, "trace": [ . < "file": "/home/shinier01/Projetos/Condivest/api-condinvest/api/app/Http/Middleware/Cors.php", "line": 18, "function": "Illuminate\\Pipeline\\", "class": "Illuminate\\Pipeline\\Pipeline", "type": "->" >, . ] >
this error in my middlaware where it was for him to do the control with the token, but I think I’m doing something wrong, it looks like this:
header('Access-Control-Allow-Origin', '*') ->header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS') ->header('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type, X-Token-Auth, Authorization'); > >
Route::middleware('auth:api')->get('/user', function (Request $request) < Route::get('/logout', 'Auth\ApiAuthController@logout')->name('logout.api'); >);
GET http://127.0.0.1:8000/api/user/logout HTTP/1.1 Content-Type: application/json Authorization: Bearer eyJ0eXA. 6c
I’ve tried both putting the «Bearer» and taking the same error. Does anyone know what I’m doing wrong? or what I forgot to install, the one to put in my code? I appreciate any tip, help or answer right now.
Why won’t my api route return anything when testing on Postman?
how come when I try to return something in the logout() function inside my api.php file ( return «testing»; , for example) I get a blank message in Postman? Here’s some high level context (my previous SO question) of what I’m trying to achieve. Before trying to achieve this — I need to make sure the API route’s working by trying to return something simple. I’m not sure why it’s not working. Thanks in advance for any input :). Here’s api.php:
Route::middleware('auth:api')->group( function () < Route::post('/logout', [RegisterController::class, 'logout']); >);
$kernel = $app->make(Kernel::class); $response = tap($kernel->handle( $request = Request::capture() ))->send(); $kernel->terminate($request, $response);
"message": "", "exception": "Symfony\\Component\\HttpKernel\\Exception\\NotFoundHttpException", "file": "/home/vagrant/myProject/vendor/laravel/framework/src/Illuminate/Routing/AbstractRouteCollection.php", "line": 43, "trace": [ < "file": "/home/vagrant/myProject/vendor/laravel/framework/src/Illuminate/Routing/CompiledRouteCollection.php", "line": 144, "function": "handleMatchedRoute", "class": "Illuminate\\Routing\\AbstractRouteCollection", "type": "->" >, < "file": "/home/vagrant/myProject/vendor/laravel/framework/src/Illuminate/Routing/Router.php", "line": 647, "function": "match", "class": "Illuminate\\Routing\\CompiledRouteCollection", "type": "->" >, < "file": "/home/vagrant/myProject/vendor/laravel/framework/src/Illuminate/Routing/Router.php", "line": 636, "function": "findRoute", "class": "Illuminate\\Routing\\Router", "type": "->" >, < "file": "/home/vagrant/myProject/vendor/laravel/framework/src/Illuminate/Routing/Router.php", "line": 625, "function": "dispatchToRoute", "class": "Illuminate\\Routing\\Router", "type": "->" >, < "file": "/home/vagrant/myProject/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php", "line": 166, "function": "dispatch", "class": "Illuminate\\Routing\\Router", "type": "->" >, < "file": "/home/vagrant/myProject/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php", "line": 128, "function": "Illuminate\\Foundation\\Http\\", "class": "Illuminate\\Foundation\\Http\\Kernel", "type": "->" >, < "file": "/home/vagrant/myProject/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php", "line": 21, "function": "Illuminate\\Pipeline\\", "class": "Illuminate\\Pipeline\\Pipeline", "type": "->" >, < "file": "/home/vagrant/myProject/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ConvertEmptyStringsToNull.php", "line": 31, "function": "handle", "class": "Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest", "type": "->" >, < "file": "/home/vagrant/myProject/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php", "line": 167, "function": "handle", "class": "Illuminate\\Foundation\\Http\\Middleware\\ConvertEmptyStringsToNull", "type": "->" >, < "file": "/home/vagrant/myProject/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php", "line": 21, "function": "Illuminate\\Pipeline\\", "class": "Illuminate\\Pipeline\\Pipeline", "type": "->" >, < "file": "/home/vagrant/myProject/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TrimStrings.php", "line": 40, "function": "handle", "class": "Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest", "type": "->" >, < "file": "/home/vagrant/myProject/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php", "line": 167, "function": "handle", "class": "Illuminate\\Foundation\\Http\\Middleware\\TrimStrings", "type": "->" >, < "file": "/home/vagrant/myProject/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ValidatePostSize.php", "line": 27, "function": "Illuminate\\Pipeline\\", "class": "Illuminate\\Pipeline\\Pipeline", "type": "->" >, < "file": "/home/vagrant/myProject/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php", "line": 167, "function": "handle", "class": "Illuminate\\Foundation\\Http\\Middleware\\ValidatePostSize", "type": "->" >, < "file": "/home/vagrant/myProject/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/PreventRequestsDuringMaintenance.php", "line": 86, "function": "Illuminate\\Pipeline\\", "class": "Illuminate\\Pipeline\\Pipeline", "type": "->" >, < "file": "/home/vagrant/myProject/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php", "line": 167, "function": "handle", "class": "Illuminate\\Foundation\\Http\\Middleware\\PreventRequestsDuringMaintenance", "type": "->" >, < "file": "/home/vagrant/myProject/vendor/fideloper/proxy/src/TrustProxies.php", "line": 57, "function": "Illuminate\\Pipeline\\", "class": "Illuminate\\Pipeline\\Pipeline", "type": "->" >, < "file": "/home/vagrant/myProject/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php", "line": 167, "function": "handle", "class": "Fideloper\\Proxy\\TrustProxies", "type": "->" >, < "file": "/home/vagrant/myProject/vendor/fruitcake/laravel-cors/src/HandleCors.php", "line": 52, "function": "Illuminate\\Pipeline\\", "class": "Illuminate\\Pipeline\\Pipeline", "type": "->" >, < "file": "/home/vagrant/myProject/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php", "line": 167, "function": "handle", "class": "Fruitcake\\Cors\\HandleCors", "type": "->" >, < "file": "/home/vagrant/myProject/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php", "line": 103, "function": "Illuminate\\Pipeline\\", "class": "Illuminate\\Pipeline\\Pipeline", "type": "->" >, < "file": "/home/vagrant/myProject/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php", "line": 141, "function": "then", "class": "Illuminate\\Pipeline\\Pipeline", "type": "->" >, < "file": "/home/vagrant/myProject/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php", "line": 110, "function": "sendRequestThroughRouter", "class": "Illuminate\\Foundation\\Http\\Kernel", "type": "->" >, < "file": "/home/vagrant/myProject/public/index.php", "line": 65, "function": "handle", "class": "Illuminate\\Foundation\\Http\\Kernel", "type": "->" > ] >