Skip to content

Debug\ExceptionHandler should have a JSON output option #26448

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
nesl247 opened this issue Mar 7, 2018 · 1 comment
Closed

Debug\ExceptionHandler should have a JSON output option #26448

nesl247 opened this issue Mar 7, 2018 · 1 comment

Comments

@nesl247
Copy link

nesl247 commented Mar 7, 2018

Q A
Bug report? no
Feature request? yes
BC Break report? no
RFC? no
Symfony version 4.0.6

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.

@yceruto
Copy link
Member

yceruto commented Apr 10, 2019

It's part of #31065 too

fabpot added a commit that referenced this issue Jun 27, 2019
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)

: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
@fabpot fabpot closed this as completed Jun 27, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants