You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It would be great if Debug\ExceptionHandler had an option to output JSON. This is more relevant than ever with the new symfony/flex ideology IMO. Symfony is being used for API development more than ever, and having HTML output while running API spec testing, dredd, behat, etc. makes it really hard to see what the issue is.
Because this could happen before the kernel is booted, my suggestion is to have the option be done when calling Debug::enable() or new Kernel() or something early on if content-negotiation is not possible.
The text was updated successfully, but these errors were encountered:
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).

:heavy_check_mark: [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](yceruto/error-handler-app@06fc647))
Commits
-------
7057244 Added ErrorHandler component
It would be great if Debug\ExceptionHandler had an option to output JSON. This is more relevant than ever with the new symfony/flex ideology IMO. Symfony is being used for API development more than ever, and having HTML output while running API spec testing, dredd, behat, etc. makes it really hard to see what the issue is.
Because this could happen before the kernel is booted, my suggestion is to have the option be done when calling
Debug::enable()
ornew Kernel()
or something early on if content-negotiation is not possible.The text was updated successfully, but these errors were encountered: