2

I want to implement logic that shows a custom 404 error page on a Symfony environment when there's no route found and a NotFoundHttpException has been thrown.

I've followed the cookbook solution line by line, also number of other sources, including StackOverflow and even trying to handle it via listener and I just can't make it work.

No matter what url I load on stage/prod it always returns 500 Server error. I've added several error pages in:

/app/Resources/TwigBundle/views/Exception/
    - error.html.twig
    - error404.html.twig
    - error500.html.twig
    - exception.html.twig
    - exception404.html.twig
    - exception500.html.twig

just to try and catch both errors and/or exceptions but there's no change. Still Server error 500 shown when trying to load http://mystage.env/non-existing-url. It never really loads any error/exception content.

The app_stage.php and app_prod.php both use false on debugger

$kernel = new AppKernel('stage', false);

I have a feeling I am missing some obvious configuration property in my parameters.yml or routing.yml or both, but I have no idea what. So far in my config.yml file part of the Twig configuration is:

# Twig Configuration
twig:
    exception_controller:  twig.controller.exception:showAction
    debug:  "%kernel.debug%"

When I dump the template, that needs to be loaded here:

# vendor/symfony/symfony/src/Symfony/Bundle/TwigBundle/Controller/ExceptionController.php

 public function showAction(
     Request $request, 
     FlattenException $exception, 
     DebugLoggerInterface $logger = null
 )
 {
    var_dump((string) $this->findTemplate(
        $request, 
        $request->getRequestFormat(), 
        $code, 
        $this->debug
    )); die;

    #...
 }

it shows the correct and expected template: string 'TwigBundle:Exception:error404.html.twig' Yet it doesn't load it, but returns 500 Server error.

For the record, I also tried using the WebfactoryExceptionsBundle, but the result was the same.

EDIT

I removed the Twig configuration line for the exception_controller, as suggested in the comments. This is what the app/logs/stage.log says:

request.ERROR: 
Uncaught PHP Exception Symfony\Component\HttpKernel\Exception\NotFoundHttpException: 
"No route found for "GET /nonexisting"" at 
/var/www/html/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/EventListener/RouterListener.php line 145 
{
 "exception":"[object] (
  Symfony\\Component\\HttpKernel\\Exception\\NotFoundHttpException(code: 0): 
    No route found for \"GET /nonexisting\" at 
    /var/www/html/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/EventListener/RouterListener.php:145, 
    Symfony\\Component\\Routing\\Exception\\ResourceNotFoundException(code: 0):  at 
    /var/www/html/app/cache/stage/appStageUrlMatcher.php:1018
 )"
} []

request.ERROR: 
Exception thrown when handling an exception (
 Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException: 
 The security context contains no authentication token. 
 One possible reason may be that there is no firewall configured for this URL.
) 
{
 "exception":"[object] (
   Symfony\\Component\\HttpKernel\\Exception\\NotFoundHttpException(code: 0): 
   No route found for \"GET /nonexisting\" at 
   /var/www/html/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/EventListener/RouterListener.php:145, 
   Symfony\\Component\\Routing\\Exception\\ResourceNotFoundException(code: 0):  at 
   /var/www/html/app/cache/stage/appStageUrlMatcher.php:1018
  )"
 } []

Same errors are logged whether I'm successfully logged or not.

Community
  • 1
  • 1
Nat Naydenova
  • 771
  • 2
  • 12
  • 30
  • Did you tried http://symfony.com/doc/current/controller/error_pages.html#testing-error-pages-during-development ? If you have an error 500 it is that your 404 template itself has an error. – COil Oct 20 '16 at 20:27
  • Have you tried testing the pages directly? `http://example.com/app_dev.php/_error/{statusCode}`, so could try `http://example.com/app_dev.php/_error/404` – Rooneyl Oct 21 '16 at 08:26
  • @Rooneyl Yes, I tried it, and it redirects me to the home page right away with no warning or error. – Nat Naydenova Oct 21 '16 at 11:27
  • if you are just adding different templates you dont need the twig configuration line. If you are using your own controller the twig config needs to reference that and not the default twig one. E.g. `AppBundle:Exception:showException` – Rooneyl Oct 21 '16 at 11:32
  • @Rooneyl I removed the twig configuration line for the exception_controller. Also tried using blank twig pages in /app/Resources/TwigBundle/views/Exception/ but still no result. – Nat Naydenova Oct 24 '16 at 08:24

0 Answers0