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

pmall's avatar

@Gewora it works fine even if the exception is coming from another class.

class TestException extends \Exception{}

class AnotherClass
{
    public function test()
    {
        throw new TestException();
    }
}

$router->get('test', function(){

    (new AnotherClass())->test();

    return redirect()->route('index')->withErrors(['something else']);

});
    /**
     * Render an exception into a response.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Exception  $e
     * @return \Illuminate\Http\Response
     */
    public function render($request, Exception $e)
    {
        if($e instanceof \TestException)
        {
            return redirect()->route('model.create')->withErrors(['test']);
        }

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

There is no bug.

You have a bit aggressive attitude although I take the time to test it on my machine.

Michael__'s avatar

In which file did you put the classes? And sorry for the bit of aggression, it just really bugs me as this issue takes up so much time :(

Thanks!

pmall's avatar

@Gewora I put the first block on routes.php and the other block on Exceptions/Handler.php class.

Does it works when you copy paste my code above ?

andreportaro's avatar

I'm having the same issue here and have to look forward, but my guess is that the view that is rendered with the error doesn't go the same way that other views (meaning that the app was never fully loaded, so probably the error bag thingy that always comes loaded with the session never got any chance to load).

Previous

Please or to participate in this conversation.