Skip to content

Commit

Permalink
minor #34197 [ErrorRenderer] Show generic message in non-debug mode (…
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
fabpot committed Nov 4, 2019
2 parents e4c6619 + 45f1a5e commit 14080ce
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 14 deletions.
Expand Up @@ -70,6 +70,6 @@ public function testDefaultJsonLoginBadRequest()

$this->assertSame(400, $response->getStatusCode());
$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));
}
}
Expand Up @@ -40,12 +40,18 @@ public function render(FlattenException $exception): string
{
$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 = [
'title' => $exception->getTitle(),
'status' => $exception->getStatusCode(),
'detail' => $message,
];
if ($debug) {
$content['detail'] = $exception->getMessage();
$content['exceptions'] = $exception->toArray();
}

Expand Down
Expand Up @@ -39,12 +39,18 @@ public static function getFormat(): string
public function render(FlattenException $exception): string
{
$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 = sprintf("[title] %s\n", $exception->getTitle());
$content .= sprintf("[status] %s\n", $exception->getStatusCode());
$content .= sprintf("[detail] %s\n", $message);

if ($debug) {
$content .= sprintf("[detail] %s\n", $exception->getMessage());

foreach ($exception->toArray() as $i => $e) {
$content .= sprintf("[%d] %s: %s\n", $i + 1, $e['class'], $e['message']);
foreach ($e['trace'] as $trace) {
Expand Down
Expand Up @@ -42,14 +42,16 @@ public function render(FlattenException $exception): string
{
$debug = $this->debug && ($exception->getHeaders()['X-Debug'] ?? true);
$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());
$charset = $this->escapeXml($this->charset);

$exceptions = '';
$message = '';
if ($debug) {
$message = '<detail>'.$this->escapeXml($exception->getMessage()).'</detail>';

$exceptions .= '<exceptions>';
foreach ($exception->toArray() as $e) {
$exceptions .= sprintf('<exception class="%s" message="%s"><traces>', $e['class'], $this->escapeXml($e['message']));
Expand All @@ -73,7 +75,7 @@ public function render(FlattenException $exception): string
<problem xmlns="urn:ietf:rfc:7807">
<title>{$title}</title>
<status>{$statusCode}</status>
{$message}
<detail>{$message}</detail>
{$exceptions}
</problem>
EOF;
Expand Down
Expand Up @@ -56,7 +56,8 @@ public function testFormatArgument()
$this->assertSame(<<<TXT
{
"title": "Internal Server Error",
"status": 500
"status": 500,
"detail": "Whoops, looks like something went wrong."
}
TXT
Expand Down
Expand Up @@ -44,7 +44,8 @@ public function getRenderData(): iterable
$expectedNonDebug = <<<JSON
{
"title": "Internal Server Error",
"status": 500
"status": 500,
"detail": "Whoops, looks like something went wrong."
}
JSON;

Expand Down
Expand Up @@ -39,6 +39,7 @@ public function getRenderData(): iterable
$expectedNonDebug = <<<TXT
[title] Internal Server Error
[status] 500
[detail] Whoops, looks like something went wrong.
TXT;

yield '->render() returns the TXT content WITH stack traces in debug mode' => [
Expand Down
Expand Up @@ -43,7 +43,7 @@ public function getRenderData(): iterable
<problem xmlns="urn:ietf:rfc:7807">
<title>Internal Server Error</title>
<status>500</status>
<detail>Whoops, looks like something went wrong.</detail>
</problem>
XML;
Expand Down
Expand Up @@ -61,7 +61,7 @@ public function getInvokeControllerDataProvider()
$request,
FlattenException::createFromThrowable(new \Exception('foo')),
500,
'{"title": "Internal Server Error","status": 500}',
'{"title": "Internal Server Error","status": 500,"detail": "Whoops, looks like something went wrong."}',
];

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

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

$request = new Request();
Expand Down

0 comments on commit 14080ce

Please sign in to comment.