chris342423
chris342423

Reputation: 451

Laravel exception handling: exception with "wrong" class

I use a single Closure to handle exceptions in my app:

App::error(function(Exception $exception, $code)
{
    if (is_a($exception, 'MsgException')) {
        ...
        return;
    }
    dd($exception); // debugging
});

The strange thing is that if I throw a MsgException...

<?php use MsgException; // alias for ExampleNamespace\MsgException

...
throw new MsgException();

...which is a custom class...

<?php namespace ExampleNamespace;

use RuntimeException;

class MsgException extends RuntimeException {}

... is_a($exception) is false and dd($exception) says it's an ErrorException.

I have no idea why this is happening. Any suggestions or ideas how I can debug my application?

Upvotes: 0

Views: 1417

Answers (2)

chris342423
chris342423

Reputation: 451

Well the short answer is pretty simple: It's not working because the exception is thrown inside of a view. You can test this by simple adding

throw new \RuntimeException('Test');

to a controller method or a routing closure or any place outside a view and by adding

<?php throw new \RuntimeException('Test'); ?>

to a view template. Laravel will show the first one as a RuntimeException and the second as an ErrorException.

Unfortunately this isn't helping to actually solve the problem.

PS: In Illuminate\View\Engines\CompilerEngine the handleViewException method replaces the original exception:

protected function handleViewException($e, $obLevel)
{
    $e = new \ErrorException($this->getMessage($e), 0, 1, $e->getFile(), $e->getLine(), $e);

    parent::handleViewException($e, $obLevel);
}

Upvotes: 1

Marwelln
Marwelln

Reputation: 29413

Don't use the general Exception handler to handle other types of exceptions. Register your own.

App::error(function(ExampleNamespace\MsgException $exception, $code)
{
    dd($exception); // debugging
});

More read about App::error are available here.

Upvotes: 1

Related Questions