Skip to content

Commit d6fd6de

Browse files
authored
Fix #50: Fix JSON rendering on JSON recursion exception
1 parent bb4ffb8 commit d6fd6de

File tree

3 files changed

+59
-15
lines changed

3 files changed

+59
-15
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## 2.0.2 under development
44

5-
- no changes in this release.
5+
- Bug #50: Fix JSON rendering on JSON recursion exception (thenotsoft)
66

77
## 2.0.1 January 26, 2022
88

src/Renderer/JsonRenderer.php

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,30 @@ final class JsonRenderer implements ThrowableRendererInterface
1919
{
2020
public function render(Throwable $t, ServerRequestInterface $request = null): ErrorData
2121
{
22-
return new ErrorData(json_encode([
23-
'message' => self::DEFAULT_ERROR_MESSAGE,
24-
], JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES));
22+
return new ErrorData(
23+
json_encode(
24+
[
25+
'message' => self::DEFAULT_ERROR_MESSAGE,
26+
],
27+
JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES
28+
)
29+
);
2530
}
2631

2732
public function renderVerbose(Throwable $t, ServerRequestInterface $request = null): ErrorData
2833
{
29-
return new ErrorData(json_encode([
30-
'type' => get_class($t),
31-
'message' => $t->getMessage(),
32-
'code' => $t->getCode(),
33-
'file' => $t->getFile(),
34-
'line' => $t->getLine(),
35-
'trace' => $t->getTrace(),
36-
], JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_INVALID_UTF8_SUBSTITUTE));
34+
return new ErrorData(
35+
json_encode(
36+
[
37+
'type' => get_class($t),
38+
'message' => $t->getMessage(),
39+
'code' => $t->getCode(),
40+
'file' => $t->getFile(),
41+
'line' => $t->getLine(),
42+
'trace' => $t->getTrace(),
43+
],
44+
JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_INVALID_UTF8_SUBSTITUTE | JSON_PARTIAL_OUTPUT_ON_ERROR
45+
)
46+
);
3747
}
3848
}

tests/Renderer/JsonRendererTest.php

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public function testRender(): void
1919
$renderer = new JsonRenderer();
2020
$data = $renderer->render(new RuntimeException());
2121

22-
$this->assertSame(json_encode(['message' => JsonRenderer::DEFAULT_ERROR_MESSAGE]), (string) $data);
22+
$this->assertSame(json_encode(['message' => JsonRenderer::DEFAULT_ERROR_MESSAGE]), (string)$data);
2323
}
2424

2525
public function testRenderVerbose(): void
@@ -36,7 +36,7 @@ public function testRenderVerbose(): void
3636
'trace' => $throwable->getTrace(),
3737
], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
3838

39-
$this->assertSame($content, (string) $data);
39+
$this->assertSame($content, (string)$data);
4040
}
4141

4242
public function testRenderVerboseWithNotUtf8String(): void
@@ -53,6 +53,40 @@ public function testRenderVerboseWithNotUtf8String(): void
5353
'trace' => $throwable->getTrace(),
5454
], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_INVALID_UTF8_SUBSTITUTE);
5555

56-
$this->assertSame($content, (string) $data);
56+
$this->assertSame($content, (string)$data);
57+
}
58+
59+
public function testRenderVerboseWithJsonRecursion(): void
60+
{
61+
$renderer = new JsonRenderer();
62+
63+
try {
64+
json_encode(
65+
new class () {
66+
public self $self;
67+
68+
public function __construct()
69+
{
70+
$this->self = $this;
71+
}
72+
},
73+
JSON_THROW_ON_ERROR
74+
);
75+
} catch (\Throwable $throwable) {
76+
$data = $renderer->renderVerbose($throwable);
77+
$content = json_encode(
78+
[
79+
'type' => \JsonException::class,
80+
'message' => $throwable->getMessage(),
81+
'code' => $throwable->getCode(),
82+
'file' => $throwable->getFile(),
83+
'line' => $throwable->getLine(),
84+
'trace' => $throwable->getTrace(),
85+
],
86+
JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_INVALID_UTF8_SUBSTITUTE | JSON_PARTIAL_OUTPUT_ON_ERROR
87+
);
88+
}
89+
90+
$this->assertSame($content, (string)$data);
5791
}
5892
}

0 commit comments

Comments
 (0)