Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 14080ce

Browse files
committed
minor #34197 [ErrorRenderer] Show generic message in non-debug mode (yceruto)
This PR was merged into the 4.4 branch. Discussion ---------- [ErrorRenderer] Show generic message in non-debug mode | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | no | New feature? | no | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - I agree with @Tobion here #34158 (comment), so let's always show the detail message, but for 5xx errors we'll send a generic message instead. /cc @dunglas wdyt? Commits ------- 45f1a5e Show generic message in non-debug mode
2 parents e4c6619 + 45f1a5e commit 14080ce

File tree

9 files changed

+31
-14
lines changed

9 files changed

+31
-14
lines changed

src/Symfony/Bundle/SecurityBundle/Tests/Functional/JsonLoginTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,6 @@ public function testDefaultJsonLoginBadRequest()
7070

7171
$this->assertSame(400, $response->getStatusCode());
7272
$this->assertSame('application/json', $response->headers->get('Content-Type'));
73-
$this->assertSame(['title' => 'Bad Request', 'status' => 400], json_decode($response->getContent(), true));
73+
$this->assertSame(['title' => 'Bad Request', 'status' => 400, 'detail' => 'Whoops, looks like something went wrong.'], json_decode($response->getContent(), true));
7474
}
7575
}

src/Symfony/Component/ErrorRenderer/ErrorRenderer/JsonErrorRenderer.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,18 @@ public function render(FlattenException $exception): string
4040
{
4141
$debug = $this->debug && ($exception->getHeaders()['X-Debug'] ?? true);
4242

43+
if ($debug) {
44+
$message = $exception->getMessage();
45+
} else {
46+
$message = 404 === $exception->getStatusCode() ? 'Sorry, the page you are looking for could not be found.' : 'Whoops, looks like something went wrong.';
47+
}
48+
4349
$content = [
4450
'title' => $exception->getTitle(),
4551
'status' => $exception->getStatusCode(),
52+
'detail' => $message,
4653
];
4754
if ($debug) {
48-
$content['detail'] = $exception->getMessage();
4955
$content['exceptions'] = $exception->toArray();
5056
}
5157

src/Symfony/Component/ErrorRenderer/ErrorRenderer/TxtErrorRenderer.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,18 @@ public static function getFormat(): string
3939
public function render(FlattenException $exception): string
4040
{
4141
$debug = $this->debug && ($exception->getHeaders()['X-Debug'] ?? true);
42+
43+
if ($debug) {
44+
$message = $exception->getMessage();
45+
} else {
46+
$message = 404 === $exception->getStatusCode() ? 'Sorry, the page you are looking for could not be found.' : 'Whoops, looks like something went wrong.';
47+
}
48+
4249
$content = sprintf("[title] %s\n", $exception->getTitle());
4350
$content .= sprintf("[status] %s\n", $exception->getStatusCode());
51+
$content .= sprintf("[detail] %s\n", $message);
4452

4553
if ($debug) {
46-
$content .= sprintf("[detail] %s\n", $exception->getMessage());
47-
4854
foreach ($exception->toArray() as $i => $e) {
4955
$content .= sprintf("[%d] %s: %s\n", $i + 1, $e['class'], $e['message']);
5056
foreach ($e['trace'] as $trace) {

src/Symfony/Component/ErrorRenderer/ErrorRenderer/XmlErrorRenderer.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,16 @@ public function render(FlattenException $exception): string
4242
{
4343
$debug = $this->debug && ($exception->getHeaders()['X-Debug'] ?? true);
4444
$title = $this->escapeXml($exception->getTitle());
45+
if ($debug) {
46+
$message = $this->escapeXml($exception->getMessage());
47+
} else {
48+
$message = 404 === $exception->getStatusCode() ? 'Sorry, the page you are looking for could not be found.' : 'Whoops, looks like something went wrong.';
49+
}
4550
$statusCode = $this->escapeXml($exception->getStatusCode());
4651
$charset = $this->escapeXml($this->charset);
4752

4853
$exceptions = '';
49-
$message = '';
5054
if ($debug) {
51-
$message = '<detail>'.$this->escapeXml($exception->getMessage()).'</detail>';
52-
5355
$exceptions .= '<exceptions>';
5456
foreach ($exception->toArray() as $e) {
5557
$exceptions .= sprintf('<exception class="%s" message="%s"><traces>', $e['class'], $this->escapeXml($e['message']));
@@ -73,7 +75,7 @@ public function render(FlattenException $exception): string
7375
<problem xmlns="urn:ietf:rfc:7807">
7476
<title>{$title}</title>
7577
<status>{$statusCode}</status>
76-
{$message}
78+
<detail>{$message}</detail>
7779
{$exceptions}
7880
</problem>
7981
EOF;

src/Symfony/Component/ErrorRenderer/Tests/Command/DebugCommandTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ public function testFormatArgument()
5656
$this->assertSame(<<<TXT
5757
{
5858
"title": "Internal Server Error",
59-
"status": 500
59+
"status": 500,
60+
"detail": "Whoops, looks like something went wrong."
6061
}
6162
6263
TXT

src/Symfony/Component/ErrorRenderer/Tests/ErrorRenderer/JsonErrorRendererTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ public function getRenderData(): iterable
4444
$expectedNonDebug = <<<JSON
4545
{
4646
"title": "Internal Server Error",
47-
"status": 500
47+
"status": 500,
48+
"detail": "Whoops, looks like something went wrong."
4849
}
4950
JSON;
5051

src/Symfony/Component/ErrorRenderer/Tests/ErrorRenderer/TxtErrorRendererTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public function getRenderData(): iterable
3939
$expectedNonDebug = <<<TXT
4040
[title] Internal Server Error
4141
[status] 500
42+
[detail] Whoops, looks like something went wrong.
4243
TXT;
4344

4445
yield '->render() returns the TXT content WITH stack traces in debug mode' => [

src/Symfony/Component/ErrorRenderer/Tests/ErrorRenderer/XmlErrorRendererTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function getRenderData(): iterable
4343
<problem xmlns="urn:ietf:rfc:7807">
4444
<title>Internal Server Error</title>
4545
<status>500</status>
46-
46+
<detail>Whoops, looks like something went wrong.</detail>
4747
4848
</problem>
4949
XML;

src/Symfony/Component/HttpKernel/Tests/Controller/ErrorControllerTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function getInvokeControllerDataProvider()
6161
$request,
6262
FlattenException::createFromThrowable(new \Exception('foo')),
6363
500,
64-
'{"title": "Internal Server Error","status": 500}',
64+
'{"title": "Internal Server Error","status": 500,"detail": "Whoops, looks like something went wrong."}',
6565
];
6666

6767
$request = new Request();
@@ -70,7 +70,7 @@ public function getInvokeControllerDataProvider()
7070
$request,
7171
FlattenException::createFromThrowable(new HttpException(405, 'Invalid request.')),
7272
405,
73-
'{"title": "Method Not Allowed","status": 405}',
73+
'{"title": "Method Not Allowed","status": 405,"detail": "Whoops, looks like something went wrong."}',
7474
];
7575

7676
$request = new Request();
@@ -79,7 +79,7 @@ public function getInvokeControllerDataProvider()
7979
$request,
8080
FlattenException::createFromThrowable(new HttpException(405, 'Invalid request.')),
8181
405,
82-
'{"title": "Method Not Allowed","status": 405}',
82+
'{"title": "Method Not Allowed","status": 405,"detail": "Whoops, looks like something went wrong."}',
8383
];
8484

8585
$request = new Request();

0 commit comments

Comments
 (0)