@@ -9,14 +9,6 @@ In Symfony applications, all errors are treated as exceptions, no matter if they
99are just a 404 Not Found error or a fatal error triggered by throwing some
1010exception in your code.
1111
12- If your app has the `TwigBundle `_ installed, a special controller handles these
13- exceptions. This controller displays debug information for errors and allows to
14- customize error pages, so run this command to make sure the bundle is installed:
15-
16- .. code-block :: terminal
17-
18- $ composer require twig
19-
2012In the :ref: `development environment <configuration-environments >`,
2113Symfony catches all the exceptions and displays a special **exception page **
2214with lots of debug information to help you discover the root problem:
@@ -39,45 +31,51 @@ Error pages for the production environment can be customized in different ways
3931depending on your needs:
4032
4133#. If you just want to change the contents and styles of the error pages to match
42- the rest of your application, :ref: `override the default error templates <use-default-exception-controller >`;
34+ the rest of your application, :ref: `override the default error templates <use-default-error-controller >`;
35+
36+ #. If you want to change the contents of non-HTML error output,
37+ :ref: `create a new normalizer <overriding-non-html-error-output >`;
4338
4439#. If you also want to tweak the logic used by Symfony to generate error pages,
45- :ref: `override the default exception controller <custom-exception -controller >`;
40+ :ref: `override the default error controller <custom-error -controller >`;
4641
4742#. If you need total control of exception handling to execute your own logic
4843 :ref: `use the kernel.exception event <use-kernel-exception-event >`.
4944
50- .. _use-default-exception -controller :
51- .. _using-the-default-exceptioncontroller :
45+ .. _use-default-error -controller :
46+ .. _using-the-default-errorcontroller :
5247
5348Overriding the Default Error Templates
5449--------------------------------------
5550
56- When the error page loads, an internal :class: `Symfony\\ Bundle\\ TwigBundle\\ Controller\\ ExceptionController `
51+ You can use the built-in Twig error renderer to override the default error
52+ templates. Both the TwigBundle and TwigBridge need to be installed for this. Run
53+ this command to ensure both are installed:
54+
55+ .. code-block :: terminal
56+
57+ $ composer require twig
58+
59+ When the error page loads, :class: `Symfony\\ Bridge\\ Twig\\ ErrorRenderer\\ TwigErrorRenderer `
5760is used to render a Twig template to show the user.
5861
5962.. _controller-error-pages-by-status-code :
6063
61- This controller uses the HTTP status code, the request format and the following
64+ This renderer uses the HTTP status code and the following
6265logic to determine the template filename:
6366
64- #. Look for a template for the given format and status code (like ``error404.json.twig ``
65- or ``error500.html.twig ``);
67+ #. Look for a template for the given status code (like ``error500.html.twig ``);
6668
6769#. If the previous template doesn't exist, discard the status code and look for
68- a generic template for the given format (like ``error.json.twig `` or
69- ``error.xml.twig ``);
70-
71- #. If none of the previous templates exist, fall back to the generic HTML template
72- (``error.html.twig ``).
70+ a generic error template (``error.html.twig ``).
7371
7472.. _overriding-or-adding-templates :
7573
7674To override these templates, rely on the standard Symfony method for
7775:ref: `overriding templates that live inside a bundle <override-templates >` and
7876put them in the ``templates/bundles/TwigBundle/Exception/ `` directory.
7977
80- A typical project that returns HTML and JSON pages might look like this:
78+ A typical project that returns HTML pages might look like this:
8179
8280.. code-block :: text
8381
@@ -87,10 +85,7 @@ A typical project that returns HTML and JSON pages might look like this:
8785 └─ Exception/
8886 ├─ error404.html.twig
8987 ├─ error403.html.twig
90- ├─ error.html.twig # All other HTML errors (including 500)
91- ├─ error404.json.twig
92- ├─ error403.json.twig
93- └─ error.json.twig # All other JSON errors (including 500)
88+ └─ error.html.twig # All other HTML errors (including 500)
9489
9590 Example 404 Error Template
9691--------------------------
@@ -112,24 +107,17 @@ To override the 404 error template for HTML pages, create a new
112107 </p>
113108 {% endblock %}
114109
115- In case you need them, the ``ExceptionController `` passes some information to
110+ In case you need them, the ``TwigErrorRenderer `` passes some information to
116111the error template via the ``status_code `` and ``status_text `` variables that
117112store the HTTP status code and message respectively.
118113
119114.. tip ::
120115
121- You can customize the status code by implementing
116+ You can customize the status code of an exception by implementing
122117 :class: `Symfony\\ Component\\ HttpKernel\\ Exception\\ HttpExceptionInterface `
123118 and its required ``getStatusCode() `` method. Otherwise, the ``status_code ``
124119 will default to ``500 ``.
125120
126- .. note ::
127-
128- The exception pages shown in the development environment can be customized
129- in the same way as error pages. Create a new ``exception.html.twig `` template
130- for the standard HTML exception page or ``exception.json.twig `` for the JSON
131- exception page.
132-
133121Security & 404 Pages
134122--------------------
135123
@@ -146,12 +134,12 @@ While you're in the development environment, Symfony shows the big *exception*
146134page instead of your shiny new customized error page. So, how can you see
147135what it looks like and debug it?
148136
149- Fortunately, the default ``ExceptionController `` allows you to preview your
137+ Fortunately, the default ``ErrorController `` allows you to preview your
150138*error * pages during development.
151139
152- To use this feature, you need to load some special routes provided by TwigBundle
140+ To use this feature, you need to load some special routes provided by FrameworkBundle
153141(if the application uses :ref: `Symfony Flex <symfony-flex >` they are loaded
154- automatically when installing Twig support ):
142+ automatically when installing `` symfony/framework-bundle `` ):
155143
156144.. configuration-block ::
157145
@@ -193,56 +181,92 @@ for a given status code as HTML or for a given status code and format.
193181 http://localhost/index.php/_error/{statusCode}
194182 http://localhost/index.php/_error/{statusCode}.{format}
195183
196- .. _custom-exception-controller :
197- .. _replacing-the-default-exceptioncontroller :
184+ .. _overriding-non-html-error-output :
185+
186+ Overriding Error output for non-HTML formats
187+ --------------------------------------------
198188
199- Overriding the Default ExceptionController
200- ------------------------------------------
189+ To override non-HTML error output, the Serializer component needs to be installed.
190+
191+ .. code-block :: terminal
192+
193+ $ composer require serializer
194+
195+ The Serializer component has a built-in ``FlattenException `` normalizer
196+ (``ProblemNormalizer ``) and JSON/XML/CSV/YAML encoders. When your application
197+ throws an exception, Symfony can output it in one of those formats. If you want
198+ to change the output contents, create a new Normalizer that supports the
199+ ``FlattenException `` input::
200+
201+ # src/App/Serializer/MyCustomProblemNormalizer.php
202+ namespace App\Serializer;
203+
204+ class MyCustomProblemNormalizer implements NormalizerInterface
205+ {
206+ public function normalize($exception, $format = null, array $context = [])
207+ {
208+ return [
209+ 'content': 'This is my custom problem normalizer.',
210+ 'exception': [
211+ 'message': $exception->getMessage(),
212+ 'code': $exception->getStatusCode(),
213+ ],
214+ ];
215+ }
216+
217+ public function supportsNormalization($data, $format = null)
218+ {
219+ return $data instanceof FlattenException;
220+ }
221+ }
222+
223+ .. _custom-error-controller :
224+ .. _replacing-the-default-errorcontroller :
225+
226+ Overriding the Default ErrorController
227+ --------------------------------------
201228
202229If you need a little more flexibility beyond just overriding the template,
203230then you can change the controller that renders the error page. For example,
204231you might need to pass some additional variables into your template.
205232
206233To do this, create a new controller anywhere in your application and set
207- the :ref: `twig.exception_controller <config-twig-exception-controller >`
234+ the :ref: `framework.error_controller <config-framework-error_controller >`
208235configuration option to point to it:
209236
210237.. configuration-block ::
211238
212239 .. code-block :: yaml
213240
214- # config/packages/twig .yaml
215- twig :
216- exception_controller : App\Controller\ExceptionController ::showAction
241+ # config/packages/framework .yaml
242+ framework :
243+ error_controller : App\Controller\ErrorController ::showAction
217244
218245 .. code-block :: xml
219246
220- <!-- config/packages/twig .xml -->
247+ <!-- config/packages/framework .xml -->
221248 <?xml version =" 1.0" encoding =" UTF-8" ?>
222249 <container xmlns =" http://symfony.com/schema/dic/services"
223250 xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
224- xmlns : twig =" http://symfony.com/schema/dic/twig"
225251 xsi : schemaLocation =" http://symfony.com/schema/dic/services
226- https://symfony.com/schema/dic/services/services-1.0.xsd
227- http://symfony.com/schema/dic/twig
228- https://symfony.com/schema/dic/twig/twig-1.0.xsd" >
252+ https://symfony.com/schema/dic/services/services-1.0.xsd" >
229253
230- <twig : config >
231- <twig : exception-controller >App\Controller\ExceptionController ::showAction</twig : exception-controller >
232- </twig : config >
254+ <framework : config >
255+ <framework : error_controller >App\Controller\ErrorController ::showAction</framework : error_controller >
256+ </framework : config >
233257
234258 </container >
235259
236260 .. code-block :: php
237261
238262 // config/packages/twig.php
239- $container->loadFromExtension('twig ', [
240- 'exception_controller ' => 'App\Controller\ExceptionController ::showAction',
263+ $container->loadFromExtension('framework ', [
264+ 'error_controller ' => 'App\Controller\ErrorController ::showAction',
241265 // ...
242266 ]);
243267
244- The :class: `Symfony\\ Component\\ HttpKernel\\ EventListener\\ ExceptionListener `
245- class used by the TwigBundle as a listener of the ``kernel.exception `` event creates
268+ The :class: `Symfony\\ Component\\ HttpKernel\\ EventListener\\ ErrorListener `
269+ class used by the FrameworkBundle as a listener of the ``kernel.exception `` event creates
246270the request that will be dispatched to your controller. In addition, your controller
247271will be passed two parameters:
248272
@@ -355,5 +379,3 @@ time and again, you can have just one (or several) listeners deal with them.
355379 your application (like :class: `Symfony\\ Component\\ Security\\ Core\\ Exception\\ AccessDeniedException `)
356380 and takes measures like redirecting the user to the login page, logging them
357381 out and other things.
358-
359- .. _`TwigBundle` : https://github.com/symfony/twig-bundle
0 commit comments