When any exception is thrown in Symfony2, the exception is caught inside the
Kernel
class and eventually forwarded to a special controller,
FrameworkBundle:Exception:show
for handling. This controller, which lives
inside the core FrameworkBundle
, determines which error template to
display and the status code that should be set for the given exception.
Tip
The customization of exception handling is actually much more powerful
than what's written here. An internal event, core.exception
, is thrown
which allows complete control over exception handling. For more information,
see events-core.exception
.
All of the error templates live inside FrameworkBundle
. To override the
templates, we simply rely on the standard method for overriding templates that
live inside a bundle. For more information, see
:ref:`overiding-bundle-templates`.
For example, to override the default error template that's shown to the
end-user, create a new template located at
app/views/FrameworkBundle/Exception/error.html.twig
:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<h1>Oops! An Error Occurred</h1>
<h2>The server returned a "{{ exception.statuscode }} {{ exception.statustext }}".</h2>
</body>
</html>
Tip
If you're not familiar with Twig, don't worry. Twig is simple, powerful
and optional templating engine that integrates with Symfony2
.
In addition to the standard HTML error page, Symfony provides a default error
page for the many of the most common response formats, including JSON
(error.json.twig
), XML, (error.xml.twig
), and even Javascript
(error.js.twig
), to name a few. To override any of these templates, just
create a new file with the same name in the
app/views/FrameworkBundle/Exception
directory. This is the standard way of
overriding any template that lives inside a bundle.
The debug-friendly exception pages shown to the developer can even be
customized in the same way by creating templates such as
exception.html.twig
for the standard HTML exception page or
exception.json.twig
for the JSON exception page.
Tip
To see the full list of default error templates, see the
Resources/views/Exception
directory of the FrameworkBundle
. In a
standard Symfony2 installation, the FrameworkBundle
can be found at
vendor/symfony/src/Symfony/Bundle/FrameworkBundle
. Often, the easiest
way to customize an error page is to copy it from the FrameworkBundle
into app/views/FrameworkBundle/Exception
and then modify it.