2

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 [] []
Tim
  • 8,036
  • 2
  • 36
  • 52
  • Just to clarify ... if your template exists ... the exception is not catched ... if it does not exist ... symfony shows the 404 error page correctly? Have you checked your logfiles if there is maybe an exception when redenering the template ? – Nicolai Fröhlich May 22 '13 at 13:29
  • app/Resources/TwigBundle/views/Exception/error404.html.twig thats the file you created ? – Nicolai Fröhlich May 22 '13 at 13:30
  • I tried with both error.html.twig and error404.html.twig. Re your clarification: yes. It works fine if I remove my custom template. – Tim May 22 '13 at 13:35
  • nothing else in logs. The uncaught exception is the one I threw myself (NotFoundHttpException) – Tim May 22 '13 at 13:37
  • What does it looks like the Twig exception controller's showAction is being executed - have you debugged it and and it is surely executed? – Nicolai Fröhlich May 22 '13 at 15:13
  • yes. I've dumped out the exception and checked it's mine right before showAction returns the new Response. It still throws, – Tim May 22 '13 at 15:27

2 Answers2

3

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.

Tim
  • 8,036
  • 2
  • 36
  • 52
0

Your problem refers to the createNotFoundException method in the symfony controller.

Symfony API - Symfony/Bundle/FrameworkBundle/Controller/Controller

Code on GitHub

To actually show a custom error template you must be using Symfony in the prod enviroment as exceptions are only caught there.

In the dev enviroment symfony uses an own modified php error handler to display the stacktrace. These exceptions are not being caught just shown by the error handler.

Have you checked that the kernel exception listener is correctly registered in the service container?

app/console container:debug --show-private
Nicolai Fröhlich
  • 51,330
  • 11
  • 126
  • 130