Accessing HTTP Status Codes on Laravel Exceptions
You are looking at a property that exists in the exception instance but cannot be accessed directly using the dot notation $exception->statusCode. This behavior stems from how PHP handles private class properties and how Laravel's error handling layer interacts with them.
The Symfony\Component\HttpKernel\Exception\NotFoundHttpException (and similar HTTP exceptions) defined in your stack do indeed contain a property named statusCode. However, notice the leading dash prefix in the debug output: -statusCode. In PHP, this indicates that the property is private. Private properties are accessible only within the scope of the class they were declared in or by classes using inheritance.
When you attempt to read $exception->statusCode inside your App\Exceptions\Handler::report method, you are stepping outside the exception's own namespace. From that vantage point, the property is invisible, and PHP throws a ErrorException stating that the property does not exist. To access this data, you must bypass the private scope restriction using reflection or leverage Laravel's built-in helper methods.
Using Reflection to Access Private Properties
The most direct way to retrieve the status code is by instantiating a ReflectionProperty object. This allows you to inspect and read properties regardless of their visibility modifiers (public, protected, or private).
Here is how you can modify your handler:
use Illuminate\Support\Facades\App;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use ReflectionClass;
use ReflectionProperty;
public function report(Throwable $exception)
{
// Only attempt to get statusCode if it's an HTTP exception
if ($this->isHttpException($exception)) {
$reflection = new ReflectionClass($exception);
try {
$property = $reflection->getProperty('statusCode');
$property->setAccessible(true); // Bypass private access
$statusCode = $property->getValue($exception);
// Now you can log or process the code
\Log::channel('mail')->error(
'HTTP Error: ' . $statusCode,
['exception' => $exception]
);
} catch (ReflectionException $e) {
// Handle cases where the property might not exist at all
\Log::error('Failed to inspect exception properties', ['message' => $e->getMessage()]);
}
}
parent::report($exception);
}
This approach is robust because it explicitly handles the reflection logic. It checks if the exception is indeed an HTTP exception using Laravel's helper method before attempting to access the specific property, preventing unnecessary processing on standard exceptions like Illuminate\Database\QueryException.
The Preferred Approach: Using the Helper Method
While reflection works, relying on private properties for application logic often signals a design smell. In many cases, you don't actually need to dig into the object's internals; Laravel provides a cleaner abstraction.
Instead of accessing the raw status code property, check if the exception is an instance of HttpException and then use the getStatusCode() method. This is the public interface intended for external consumption.
public function report(Throwable $exception)
{
if ($this->isHttpException($exception)) {
// Laravel's HttpException implements getStatusCode()
// which internally retrieves the statusCode property
$statusCode = $exception->getStatusCode();
\Log::error('HTTP Error occurred', [
'code' => $statusCode,
'message' => $exception->getMessage(),
]);
}
parent::report($exception);
}
This pattern is significantly more maintainable. If the underlying Symfony exception class changes its internal storage mechanism for status codes in a future version of Laravel or PHP, your public method contract remains untouched. You avoid tight coupling with private implementation details.
When to Use Which
Use reflection only when you are dealing with legacy codebases or specific scenarios where the public interface is insufficientâfor instance, if you need to inspect properties that lack public getters entirely and no alternative exists. However, for standard status code retrieval, always prefer getStatusCode().
Remember that accessing private properties via reflection can introduce performance overhead in high-frequency loops, though this is negligible in a single request handler context. The real trade-off here is maintainability versus direct access. By sticking to public methods, your code remains decoupled from the internal implementation of the framework's exception classes.
If you are building custom middleware or error reporters that need to inspect deep into the exception stack, understanding these visibility rules is crucial. For general logging purposes, leveraging Laravel's standard helpers ensures compatibility across versions and reduces the risk of breaking changes in your application logic.