Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 error 500 instead of 404 at production

Tags:

symfony

In my Symfony2 project I'm getting at development mode correct 404 Exception screen. But I'm getting blank screen with HTTP status code 500 instead of 404 at production mode. I'm using custom error templates located in app/Resources/TwigBundle/views/Exception. In apache error log it creates this message:

PHP Fatal error:  Uncaught exception 'Symfony\\Component\\Routing\\Exception\\ResourceNotFoundException' in /home/test/app/cache/prod/appprodUrlMatcher.php:518\nStack trace:
#0 /home/test/app/cache/prod/classes.php(1025): appprodUrlMatcher->match('/404')
#1 /home/test/app/cache/prod/classes.php(4550): Symfony\\Component\\Routing\\Router->match('/404')
#2 [internal function]: Symfony\\Bundle\\FrameworkBundle\\EventListener\\RouterListener->onKernelRequest(Object(Symfony\\Component\\HttpKernel\\Event\\GetResponseEvent))
#3 /home/test/app/cache/prod/classes.php(3777): call_user_func(Array, Object(Symfony\\Component\\HttpKernel\\Event\\GetResponseEvent))
#4 /home/test/app/cache/prod/classes.php(3703): Symfony\\Component\\EventDispatcher\\EventDispatcher->doDispatch(Array, 'kernel.request', Object(Symfony\\Component\\HttpKernel\\Event\\GetResponseEvent))
#5 /home/test/app/cache/prod/classes.php(4787): Symfony\\Component\\EventDispatcher\\EventDispatcher->dispatch('kernel.request', Object(Symfony\\Component\\HttpKernel\\Event\\Get in /home/test/app/cache/prod/classes.php on line 4560
like image 869
kuboslav Avatar asked May 03 '12 07:05

kuboslav


3 Answers

Symfony\Component\Routing\Exception\ResourceNotFoundException means undefined route name. It looks like you've got somewhere in your error template {{ path('wrong_route') }}.

like image 189
jkucharovic Avatar answered Nov 12 '22 15:11

jkucharovic


The most likely reason you're getting a 500 error / blank page on production (app.php), even when you've defined a custom error page ( e.g. app/Resources/TwigBundle/views/Exception/error404.html.twig ) is that your error template is calling the is_granted twig function, without checking if the user is logged in.

Steps to debug:

1)Check app/logs/prod.log. Do you see an error like this?

request.ERROR: Exception thrown when handling an exception (Twig_Error_Runtime: An exception has been thrown during the rendering of a template ("The security context contains no authentication token. One possible reason may be that there is no firewall configured for this URL.")

2)If you see the error mentioned above, see if you can find a reference to is_granted in your error template. Check that the user is logged in before calling is_granted. E.g.:

{% if app.user is not null and is_granted('ROLE_ADMIN') %}
<p>Text goes here</p>
{% else %}

3)If you can't find a reference to is_granted in your template, see if there's a call to knp_menu_render(), which is used by the KNP menu bundle. Check in any extended template as well. Wrap the call to knp_menu_render in a check to verify that the user is logged in:

{% if app.user %}
    {{ knp_menu_render() }}
{% endif %}

For more information, check the comment by stof at the end of this page: https://github.com/symfony/symfony/issues/5320

like image 10
Jay Sheth Avatar answered Nov 12 '22 16:11

Jay Sheth


In my case it was use of {% stylesheets %} tag in a 404 template, while TwigBundle was not included in the Assetic config.

Check your app/logs/prod.log, it should have an answer.

like image 5
Konstantin Pereiaslov Avatar answered Nov 12 '22 14:11

Konstantin Pereiaslov