Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

richartkeil's avatar

Exception Handler not recognized

Hi there, I'm currently trying to log an error with the Exception Handler Class. But for some reason, Laravel acts like there is no such class. Composers autoload is dumped.

Here's the controller action that causes a test error:

public function causeTestError()
    {
        throw new \Exception('This is an error.');
    }

And here the handler class in app/Exceptions/Handler.php:

<?php

namespace App\Exceptions;

use Exception;
use Illuminate\Session\TokenMismatchException;
use Illuminate\Validation\ValidationException;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;

class Handler extends ExceptionHandler
{
    protected $dontReport = [
        AuthorizationException::class,
        HttpException::class,
        ModelNotFoundException::class,
        TokenMismatchException::class,
        ValidationException::class,
    ];

    public function report(Exception $e)
    {
        dd('report');
        parent::report($e);
    }

    public function render($request, Exception $e)
    {
        dd('render');
        return parent::render($request, $e);
    }
}

Instead of showing report or render it just displays the typical Laravel exception view with the message from above This is an error..

What am I missing here?

Thanks in advance, Richard

0 likes
6 replies
ejdelmonico's avatar

Here is what I use

public function render($request, Exception $exception)
    {
        if ($this->isHttpException($exception)) {
            if (view()->exists('errors.'.$exception->getStatusCode())) {
                return response()->view('errors.'.$exception->getStatusCode(), [], $exception->getStatusCode());
            }
        }
        if (!$this->isHttpException($exception)) {
            $exception = new \Symfony\Component\HttpKernel\Exception\HttpException(500);
        }

        return parent::render($request, $exception);
    }

Then place your custom blade error pages in views/errors and name them according to error like 500.blade.php.

richartkeil's avatar

Thanks! But it doesn't even reach the render method to execute the code.

Do you have to configure the handler somewhere?

Cheers, Richard

ejdelmonico's avatar

True, I just posted a starting point. You will have to write your own code. You can put whatever message you want in the blade pages including the use of custom messages. For instance, the authorized users name or whatever.

richartkeil's avatar

Yeah, but I don't want to display the error in a blade template.

My current exception never reaches the report and render methods, otherwise it would display the outputs by dd(), wouldn't it? I just need the exceptions to reach the handler, because in the moment it is simply ignored.

Richard

richartkeil's avatar
richartkeil
OP
Best Answer
Level 1

Ok, I've found the solution. In bootstrap/app.php:

$app->singleton(
    Illuminate\Contracts\Debug\ExceptionHandler::class,
    App\Exceptions\Handler::class
);

was changed to

$app->singleton(
    Illuminate\Contracts\Debug\ExceptionHandler::class,
    Illuminate\Foundation\Exceptions\Handler::class
);

Therefore the basic error handler of Laravel was used. Thanks to @ejdelmonico for the help :)

Cheers, Richard

1 like
patackett's avatar

Hello everyone. I am having the same issue in Laravel 5.5. my bootstrap/app.php file is configured like this:

$app->singleton( Illuminate\Contracts\Debug\ExceptionHandler::class, App\Exceptions\Handler::class );

However, this does not execute the render or report functions in App\Exceptions\Handler. Instead, the basic error handler in Illuminate\Foundation\Exceptions\Handler::class gets executed.

Any suggestions would be greatly appreciated. Thanks!

Please or to participate in this conversation.