13a5e2dd1f
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]( |
||
---|---|---|
.. | ||
Bundle | ||
CacheClearer | ||
CacheWarmer | ||
Config | ||
Controller | ||
ControllerMetadata | ||
DataCollector | ||
Debug | ||
DependencyInjection | ||
Event | ||
EventListener | ||
Exception | ||
Fixtures | ||
Fragment | ||
HttpCache | ||
Log | ||
Profiler | ||
HttpKernelBrowserTest.php | ||
HttpKernelTest.php | ||
KernelTest.php | ||
Logger.php | ||
TestHttpKernel.php | ||
UriSignerTest.php |