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

Michael__'s avatar

Additional Exception Handler in L5

Hey,

how can I add an additional exception handler at a different location?

I tried this:

<?php namespace Modules\Gewora\Exceptions;

use Exception;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Session\TokenMismatchException;

class Handler extends ExceptionHandler
{
    /**
     * A list of the exception types that should not be reported.
     *
     * @var array
     */
    protected $dontReport = [
        'Symfony\Component\HttpKernel\Exception\HttpException'
    ];

    /**
     * Report or log an exception.
     *
     * This is a great spot to send exceptions to Sentry, Bugsnag, etc.
     *
     * @param  \Exception  $e
     * @return void
     */
    public function report(Exception $e)
    {
        dd('Test1');
        if($e instanceof TokenMismatchException) {
            dd('Test2');
        }

        return parent::report($e);
    }

    /**
     * Render an exception into an HTTP response.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Exception  $e
     * @return \Illuminate\Http\Response
     */
    public function render($request, Exception $e)
    {
        if ($this->isHttpException($e))
        {
            return $this->renderHttpException($e);
        }
        else
        {
            return parent::render($request, $e);
        }
    }
}

But it does not catch the exception, it does not even go into the report method even though the file has been loaded correctly. What am I doing wrong?

Thanks!

0 likes
17 replies
Michael__'s avatar

I assume it is cause of the render method in the original Handler, right? How can I fix that without touching the original Handler?

Thanks!

usman's avatar

Afaik, it is not possible to have two kernel level exception handlers. You can override the current one though.

Refer to your laravelinstallation/bootstrap/app.php file and you will find the following code:

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

Usman.

Michael__'s avatar

Is there a way to do that without touching any of the original files? The system should be module based and therefore should not touch the original files, just to catch an Exception.

Thank you!

usman's avatar

You can use middlewares for this purpose IMHO.

Michael__'s avatar

Would you mind giving me an example? Not sure how that would be done with Middleware :/

usman's avatar

Maybe something like this:

use Closure;

class SomeMiddleware {

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {   
        try
        {
            $response = $next($request);
        }
        catch(ModuleException $e)
        {

        }

        return $response;
    }

}

You will still be needed to specify the middleware inside the $middleware array of the App\Http\Kernel class.

Michael__'s avatar

Ah I see, thanks. There is really no way to handle a simple exception without touching the main files? Can't really believe that. Maybe someone else can come up with a hint for that.

Thanks!

usman's avatar

You can always use inline catch blocks :)

Regards!

Michael__'s avatar

Yeah, but I hoped that I can avoid that, especially because I can't (technically I can, but i would rather play with spiders. Eww, spiders) do that cause a TokenMismatchException can be thrown at so many places. Will wait a bit, maybe someone has another idea. In L4 this was not a big deal cause of App::error() :/

Thank you very much! :)

pmall's avatar

What if you make a service provider for your module and erase this binding :

public function register()
{
  $this->app->singleton(
    'Illuminate\Contracts\Debug\ExceptionHandler',
    'Modules\Gewora\Exceptions'
  );
}

Dont know if it works but I think it is worth trying.

Of course if you want to keep the original exception handler functionalities you have to extends it (App\Exceptions\Handler) and call the parent methods in your custom exception handler.

1 like
Michael__'s avatar

It does not seem to catch it :(

TokenMismatchException in VerifyCsrfToken.php line 46

Thank you!

pmall's avatar

@Gewora What ? This is absolutely not related with CSRF verification. Check again.

pmall's avatar

Did you registered your module's service provider in config/app.php ?

Michael__'s avatar

Not directly in the app.php but it is being registered through the modules plugin.

Michael__'s avatar

Yeah I use a modules package which handles the registration of the ServiceProvider in the corresponding module. The ServiceProvider is registered for sure.

Please or to participate in this conversation.