I have a patch to add some custom handling of 500 errors that run through the code in errors.inc, so that it returns a themed page in those cases.

This works well in certain conditions, but I have an error that occurred with a misnamed twig template, so I got

Twig_Error_Loader: Template "@mytheme/search-result/search-result--event.twig" is not defined (Drupal\Core\Template\Loader\ThemeRegistryLoader: Unable to find template "@mytheme/search-result/search-result--event.twig" in the Drupal theme registry.) in "themes/custom/mytheme/templates/node/node--admissions-event--search-result.html.twig" at line 1. in Twig_Loader_Chain->getCacheKey() (line 129 of /var/www/mytheme-qa/site/vendor/twig/twig/lib/Twig/Loader/Chain.php).

After some digging I see that this error is generated here: /core/lib/Drupal/Core/Template/Loader/ThemeRegistryLoader.php

if ($throw) {
  throw new \Twig_Error_Loader(sprintf('Unable to find template "%s" in the Drupal theme registry.', $name));

The problem is this type of error isn't handled by Drupal. It's a Twig engine error. So an exception subscriber doesn't catch this.

Any suggestions on how to return a themed 500 page in fatal error cases like this at the theme layer?

1 Answer 1


Just checked, there is no special handling of Twig exceptions in core, so they end up in FinalExceptionSubscriber. You can add a custom exception subscriber catching Twig_Error_Loader or Twig_Error_Syntax and return a themed response (though you might need to theme this yourself when the theme layer is broken because of the Twig error).

error.inc has nothing to do with this as long as there is no fatal error not catchable by Symfony (but this would be a failure of the Twig layer, which normally protects you from low level errors)

  • Great this works! I still need my patch to errors.inc though, as uncaught fatal errors end up there, but this is working much better than what I previously had, because it's catching additional errors, such as twig errors.
    – oknate
    Feb 5, 2018 at 17:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.