How do I get the HTTP Status Code from an Exception thrown in Laravel?
Stefan Izdrail
Founder & Senior Architect · 2026-06-29
Title: Accessing HTTP Status Code from Exceptions in Laravel Applications
Introduction:
In Laravel applications, handling exceptions is a crucial part of error management. The framework offers an excellent way to do so by using the Exception Handler located in App/Exceptions/Handler.php. Within this file, you can define how your application should respond to different types of errors and exceptions. One important aspect of exception handling is checking the HTTP status code that corresponds to each exception or error type, which enables developers to display specific messages or implement custom logic based on the response. In this blog post, we will discuss how to access the HTTP Status Code in Laravel's Exception Handler.
Accessing the HTTP Status Code:
Firstly, ensure you have a clear understanding of what HTTP status codes are and their respective purposes. These codes are three-digit numeric codes defined by the Hypertext Transfer Protocol (HTTP) specification to indicate the reason for a particular response from a server. For instance, 200 for success, 301 for permanent redirection, 403 for forbidden access, and 404 for not found.
To access the HTTP status code from an exception in Laravel:
1. Ensure you have defined your custom exceptions or extended the Exception class as needed. For example, if you want to create a new exception called CustomNotFoundException for custom 404 errors, extend App\Exceptions\Handler and define the following:
class CustomNotFoundException extends \Symfony\Component\HttpKernel\Exception\NotFoundHttpException {
public function __construct($message = null)
{
parent::__construct($message);
$this->statusCode = 404; // Set the HTTP status code explicitly.
}
}
2. In your App/Exceptions/Handler.php file, define your custom exception handling logic. For instance:
public function report(Throwable $exception)
{
if ($exception instanceof CustomNotFoundException) {
return response()->view('errors.404', [], 404); // Render the appropriate error view for the status code defined in your custom exception.
} elseif ($exception instanceof SomeOtherErrorType) {
return response()->json(['error' => $exception->getMessage()], $exception->statusCode); // Handle other exceptions similarly, accessing their specific status codes.
} else {
parent::report($exception); // Report remaining errors as usual.
}
}
3. Test your custom exception handling by triggering a CustomNotFoundException or any other defined exception type in your application code, and verify that the appropriate status codes are being reported and handled correctly.
Remember to always follow best practices when coding in Laravel, such as using descriptive error messages, defining meaningful exception classes for different error types, and enforcing proper HTTP status codes. This will facilitate better communication with clients or other developers who may need to debug your application in the future.
Conclusion:
In this blog post, we have discussed how to access and handle HTTP status codes from exceptions in Laravel applications through custom exception handling and error management techniques. By following these guidelines and best practices, you can ensure a more robust and maintainable Laravel project that efficiently manages errors and provides clear information for users.