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 https://github.com/symfony/symfony/pull/34158#issuecomment-548181099, so let's always show the detail message, but for 5xx errors we'll send a generic message instead.

/cc @dunglas wdyt?

Commits
-------

45f1a5ee06 Show generic message in non-debug mode
This commit is contained in:
Fabien Potencier 2019-11-04 13:48:39 +01:00
commit 14080ce5b5
9 changed files with 32 additions and 15 deletions

View File

@ -70,6 +70,6 @@ class JsonLoginTest extends AbstractWebTestCase
$this->assertSame(400, $response->getStatusCode()); $this->assertSame(400, $response->getStatusCode());
$this->assertSame('application/json', $response->headers->get('Content-Type')); $this->assertSame('application/json', $response->headers->get('Content-Type'));
$this->assertSame(['title' => 'Bad Request', 'status' => 400], json_decode($response->getContent(), true)); $this->assertSame(['title' => 'Bad Request', 'status' => 400, 'detail' => 'Whoops, looks like something went wrong.'], json_decode($response->getContent(), true));
} }
} }

View File

@ -40,12 +40,18 @@ class JsonErrorRenderer implements ErrorRendererInterface
{ {
$debug = $this->debug && ($exception->getHeaders()['X-Debug'] ?? true); $debug = $this->debug && ($exception->getHeaders()['X-Debug'] ?? true);
if ($debug) {
$message = $exception->getMessage();
} else {
$message = 404 === $exception->getStatusCode() ? 'Sorry, the page you are looking for could not be found.' : 'Whoops, looks like something went wrong.';
}
$content = [ $content = [
'title' => $exception->getTitle(), 'title' => $exception->getTitle(),
'status' => $exception->getStatusCode(), 'status' => $exception->getStatusCode(),
'detail' => $message,
]; ];
if ($debug) { if ($debug) {
$content['detail'] = $exception->getMessage();
$content['exceptions'] = $exception->toArray(); $content['exceptions'] = $exception->toArray();
} }

View File

@ -39,12 +39,18 @@ class TxtErrorRenderer implements ErrorRendererInterface
public function render(FlattenException $exception): string public function render(FlattenException $exception): string
{ {
$debug = $this->debug && ($exception->getHeaders()['X-Debug'] ?? true); $debug = $this->debug && ($exception->getHeaders()['X-Debug'] ?? true);
$content = sprintf("[title] %s\n", $exception->getTitle());
$content .= sprintf("[status] %s\n", $exception->getStatusCode());
if ($debug) { if ($debug) {
$content .= sprintf("[detail] %s\n", $exception->getMessage()); $message = $exception->getMessage();
} else {
$message = 404 === $exception->getStatusCode() ? 'Sorry, the page you are looking for could not be found.' : 'Whoops, looks like something went wrong.';
}
$content = sprintf("[title] %s\n", $exception->getTitle());
$content .= sprintf("[status] %s\n", $exception->getStatusCode());
$content .= sprintf("[detail] %s\n", $message);
if ($debug) {
foreach ($exception->toArray() as $i => $e) { foreach ($exception->toArray() as $i => $e) {
$content .= sprintf("[%d] %s: %s\n", $i + 1, $e['class'], $e['message']); $content .= sprintf("[%d] %s: %s\n", $i + 1, $e['class'], $e['message']);
foreach ($e['trace'] as $trace) { foreach ($e['trace'] as $trace) {

View File

@ -42,14 +42,16 @@ class XmlErrorRenderer implements ErrorRendererInterface
{ {
$debug = $this->debug && ($exception->getHeaders()['X-Debug'] ?? true); $debug = $this->debug && ($exception->getHeaders()['X-Debug'] ?? true);
$title = $this->escapeXml($exception->getTitle()); $title = $this->escapeXml($exception->getTitle());
if ($debug) {
$message = $this->escapeXml($exception->getMessage());
} else {
$message = 404 === $exception->getStatusCode() ? 'Sorry, the page you are looking for could not be found.' : 'Whoops, looks like something went wrong.';
}
$statusCode = $this->escapeXml($exception->getStatusCode()); $statusCode = $this->escapeXml($exception->getStatusCode());
$charset = $this->escapeXml($this->charset); $charset = $this->escapeXml($this->charset);
$exceptions = ''; $exceptions = '';
$message = '';
if ($debug) { if ($debug) {
$message = '<detail>'.$this->escapeXml($exception->getMessage()).'</detail>';
$exceptions .= '<exceptions>'; $exceptions .= '<exceptions>';
foreach ($exception->toArray() as $e) { foreach ($exception->toArray() as $e) {
$exceptions .= sprintf('<exception class="%s" message="%s"><traces>', $e['class'], $this->escapeXml($e['message'])); $exceptions .= sprintf('<exception class="%s" message="%s"><traces>', $e['class'], $this->escapeXml($e['message']));
@ -73,7 +75,7 @@ class XmlErrorRenderer implements ErrorRendererInterface
<problem xmlns="urn:ietf:rfc:7807"> <problem xmlns="urn:ietf:rfc:7807">
<title>{$title}</title> <title>{$title}</title>
<status>{$statusCode}</status> <status>{$statusCode}</status>
{$message} <detail>{$message}</detail>
{$exceptions} {$exceptions}
</problem> </problem>
EOF; EOF;

View File

@ -56,7 +56,8 @@ TXT
$this->assertSame(<<<TXT $this->assertSame(<<<TXT
{ {
"title": "Internal Server Error", "title": "Internal Server Error",
"status": 500 "status": 500,
"detail": "Whoops, looks like something went wrong."
} }
TXT TXT

View File

@ -44,7 +44,8 @@ JSON;
$expectedNonDebug = <<<JSON $expectedNonDebug = <<<JSON
{ {
"title": "Internal Server Error", "title": "Internal Server Error",
"status": 500 "status": 500,
"detail": "Whoops, looks like something went wrong."
} }
JSON; JSON;

View File

@ -39,6 +39,7 @@ TXT;
$expectedNonDebug = <<<TXT $expectedNonDebug = <<<TXT
[title] Internal Server Error [title] Internal Server Error
[status] 500 [status] 500
[detail] Whoops, looks like something went wrong.
TXT; TXT;
yield '->render() returns the TXT content WITH stack traces in debug mode' => [ yield '->render() returns the TXT content WITH stack traces in debug mode' => [

View File

@ -43,7 +43,7 @@ XML;
<problem xmlns="urn:ietf:rfc:7807"> <problem xmlns="urn:ietf:rfc:7807">
<title>Internal Server Error</title> <title>Internal Server Error</title>
<status>500</status> <status>500</status>
<detail>Whoops, looks like something went wrong.</detail>
</problem> </problem>
XML; XML;

View File

@ -61,7 +61,7 @@ class ErrorControllerTest extends TestCase
$request, $request,
FlattenException::createFromThrowable(new \Exception('foo')), FlattenException::createFromThrowable(new \Exception('foo')),
500, 500,
'{"title": "Internal Server Error","status": 500}', '{"title": "Internal Server Error","status": 500,"detail": "Whoops, looks like something went wrong."}',
]; ];
$request = new Request(); $request = new Request();
@ -70,7 +70,7 @@ class ErrorControllerTest extends TestCase
$request, $request,
FlattenException::createFromThrowable(new HttpException(405, 'Invalid request.')), FlattenException::createFromThrowable(new HttpException(405, 'Invalid request.')),
405, 405,
'{"title": "Method Not Allowed","status": 405}', '{"title": "Method Not Allowed","status": 405,"detail": "Whoops, looks like something went wrong."}',
]; ];
$request = new Request(); $request = new Request();
@ -79,7 +79,7 @@ class ErrorControllerTest extends TestCase
$request, $request,
FlattenException::createFromThrowable(new HttpException(405, 'Invalid request.')), FlattenException::createFromThrowable(new HttpException(405, 'Invalid request.')),
405, 405,
'{"title": "Method Not Allowed","status": 405}', '{"title": "Method Not Allowed","status": 405,"detail": "Whoops, looks like something went wrong."}',
]; ];
$request = new Request(); $request = new Request();