Show generic message in non-debug mode

This commit is contained in:
Yonel Ceruto 2019-10-30 22:35:27 -04:00
parent aea43b27b0
commit 45f1a5ee06
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('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);
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();
}

View File

@ -39,12 +39,18 @@ class TxtErrorRenderer implements ErrorRendererInterface
public function render(FlattenException $exception): string
{
$debug = $this->debug && ($exception->getHeaders()['X-Debug'] ?? true);
$content = sprintf("[title] %s\n", $exception->getTitle());
$content .= sprintf("[status] %s\n", $exception->getStatusCode());
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) {
$content .= sprintf("[%d] %s: %s\n", $i + 1, $e['class'], $e['message']);
foreach ($e['trace'] as $trace) {

View File

@ -42,14 +42,16 @@ class XmlErrorRenderer implements ErrorRendererInterface
{
$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']));
@ -73,7 +75,7 @@ class XmlErrorRenderer implements ErrorRendererInterface
<problem xmlns="urn:ietf:rfc:7807">
<title>{$title}</title>
<status>{$statusCode}</status>
{$message}
<detail>{$message}</detail>
{$exceptions}
</problem>
EOF;

View File

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

View File

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

View File

@ -39,6 +39,7 @@ TXT;
$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' => [

View File

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

View File

@ -61,7 +61,7 @@ class ErrorControllerTest extends TestCase
$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();
@ -70,7 +70,7 @@ class ErrorControllerTest extends TestCase
$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();
@ -79,7 +79,7 @@ class ErrorControllerTest extends TestCase
$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();