Search code examples
symfonysymfony-2.2

Why are exceptions not caught when overriding error templates?


I've followed the Symfony2.2 documentation on customising error templates and overridden the default error template at app/Resources/TwigBundle/views/Exception/error.html.twig as described for Twig.

In some controllers I am throwing a custom 404 when a DB entity doesn't exist for example. I'm doing that with:

throw $this->createNotFoundException('Thing not found');

When my custom template exists this exception is not being caught, so my error page is never rendered and I get a fatal error. I've not changed anything to do with exception handlers, so I don't understand why overriding the template breaks Symfony error rendering functionality.

It looks like the Twig exception controller's showAction is being executed, but the exception still gets thrown out of the stack.

What needs to be done to ensure the exception is caught and rendered?

Additional info:

The following is logged. (changed name of client only)

[2013-05-22 16:26:22] request.INFO: Matched route "show_thing" (parameters: "_controller": "Client\SiteBundle\Controller\ThingController::indexAction", "id": "thing", "_route": "show_thing") [] []
[2013-05-22 16:26:22] security.INFO: No expression found; abstaining from voting. [] []
[2013-05-22 16:26:22] request.ERROR: Uncaught PHP Exception Symfony\Component\HttpKernel\Exception\NotFoundHttpException: "thing not found" at /home/vhosts/client/symfony/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php line 149 [] []

Solution

  • I worked it out.

    My template had a block that was not defined in the layout.

    This error was not logged, but was enough to break the rendering,

    Thanks to @nifr for all your help.