minor #32359 [ErrorCatcher] Pretty print JSON formatted errors (javiereguiluz)

This PR was merged into the 4.4 branch.

Discussion
----------

[ErrorCatcher] Pretty print JSON formatted errors

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | not needed

A long string with JSON contents is not very useful to quickly parse the error contents, so I propose to "pretty print" the JSON errors.

Commits
-------

ab926d2 [ErrorCatcher] Pretty print JSON formatted errors
This commit is contained in:
Yonel Ceruto 2019-07-04 11:27:52 -04:00
commit 0151279cfe
2 changed files with 12 additions and 2 deletions

View File

@ -47,6 +47,6 @@ class JsonErrorRenderer implements ErrorRendererInterface
$content['exceptions'] = $exception->toArray();
}
return (string) json_encode($content);
return (string) json_encode($content, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_LINE_TERMINATORS | JSON_PRESERVE_ZERO_FRACTION);
}
}

View File

@ -20,7 +20,17 @@ class JsonErrorRendererTest extends TestCase
public function testRender()
{
$exception = FlattenException::createFromThrowable(new \RuntimeException('Foo'));
$expected = '{"title":"Internal Server Error","status":500,"detail":"Foo","exceptions":[{"message":"Foo","class":"RuntimeException","trace":';
$expected = <<<JSON
{
"title": "Internal Server Error",
"status": 500,
"detail": "Foo",
"exceptions": [
{
"message": "Foo",
"class": "RuntimeException",
"trace":
JSON;
$this->assertStringStartsWith($expected, (new JsonErrorRenderer())->render($exception));
}