This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/HttpKernel/Tests
Fabien Potencier 13a5e2dd1f feature #31065 Add ErrorHandler component (yceruto)
This PR was merged into the 4.4 branch.

Discussion
----------

Add ErrorHandler component

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | yes
| Tests pass?   | no
| Fixed tickets | #25905, #26448
| License       | MIT
| Doc PR        | TODO

Mainly for API-based apps that don't require TwigBundle to get the correct exception response according to the request format (aka `_format` attribute).

![exception_response](https://user-images.githubusercontent.com/2028198/55509651-713dc700-562a-11e9-8b98-bef3b0229397.gif)

✔️ [RFC7807](https://tools.ietf.org/html/rfc7807) compliant for JSON and XML formats.

---

This introduce a new `ErrorRenderer` service that render a `FlattenException` into a given format:
```php
use Symfony\Component\ErrorHandler\ErrorRenderer\ErrorRenderer;
use Symfony\Component\ErrorHandler\ErrorRenderer\HtmlErrorRenderer;
use Symfony\Component\ErrorHandler\ErrorRenderer\JsonErrorRenderer;

$renderers = [
    new HtmlErrorRenderer(),
    new JsonErrorRenderer(),
    // ...
];
$errorRenderer = new ErrorRenderer($renderers);

return new Response(
    $errorRenderer->render($exception, $request->getRequestFormat()),
    $exception->getStatusCode(),
    $exception->getHeaders()
);
```

The built-in error renderers are:

| Format | Class |
| --- | --- |
| html | HtmlErrorRenderer |
| json | JsonErrorRenderer |
| xml, atom | XmlErrorRenderer |
| txt | TxtErrorRenderer |

And you can add your own error renderer by implementing the `ErrorRendererInterface` and tagging it with `error_handler.renderer` in your service definition.

Creating your own error renderer for a built-in format will end up replacing the related built-in error renderer.

Demo: https://github.com/yceruto/error-handler-app ([add custom error renderer](06fc647841))

Commits
-------

7057244890 Added ErrorHandler component
2019-06-27 14:40:37 +02:00
..
Bundle [HttpKernel] Remove convention based commands registration 2017-08-10 19:24:31 +02:00
CacheClearer Merge branch '4.1' into 4.2 2019-01-16 21:31:39 +01:00
CacheWarmer Merge branch '3.4' into 4.2 2019-05-30 18:06:08 +02:00
Config Merge branch '4.2' into 4.3 2019-05-30 18:10:05 +02:00
Controller use proper return types in ErrorHandler and ArgumentResolver 2019-06-24 02:04:42 +01:00
ControllerMetadata Merge branch '3.4' into 4.1 2019-01-16 19:21:11 +01:00
DataCollector Added ErrorHandler component 2019-06-20 11:01:49 -04:00
Debug Merge branch '4.2' into 4.3 2019-05-30 18:10:05 +02:00
DependencyInjection Merge branch '4.2' into 4.3 2019-05-30 18:10:05 +02:00
Event fixed CS 2019-03-22 07:07:54 +01:00
EventListener Added ErrorHandler component 2019-06-20 11:01:49 -04:00
Exception [HttpKernel] change $previous argument for HttpException to \Throwable 2019-03-30 08:30:07 +01:00
Fixtures Merge branch '4.2' into 4.3 2019-06-13 13:01:17 +02:00
Fragment Merge branch '4.2' into 4.3 2019-05-30 18:10:05 +02:00
HttpCache Merge branch '4.2' into 4.3 2019-05-30 18:10:05 +02:00
Log Use willReturn() instead of will(returnValue()). 2019-05-30 17:47:52 +02:00
Profiler Merge branch '4.1' into 4.2 2019-01-16 21:31:39 +01:00
HttpKernelBrowserTest.php Merge branch '4.2' into 4.3 2019-05-30 18:10:05 +02:00
HttpKernelTest.php Merge branch '4.2' into 4.3 2019-05-30 18:10:05 +02:00
KernelTest.php Merge branch '4.2' into 4.3 2019-05-30 18:10:05 +02:00
Logger.php switched array() to [] 2019-01-16 10:39:14 +01:00
TestHttpKernel.php switched array() to [] 2019-01-16 10:39:14 +01:00
UriSignerTest.php [HttpKernel] Correctly Render Signed URIs Containing Fragments 2018-12-24 13:29:26 +00:00