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

Skip to content

[ErrorRenderer] Show generic message in non-debug mode #34197

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
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));
}
}
Original file line number Diff line number Diff line change
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.';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry but I'm totally👎 on this. The message does not make any sense in an API context.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you like a generic message like: Not Found (404 error) or do you have any other message in mind?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer no message at all. What we put here it's arbitrary.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other suggestion for this 404 message is this one:

The server has not found anything matching the Request-URI

Taken from https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

Copy link
Member Author

@yceruto yceruto Nov 4, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is how the message would look for JSON format (non-debug mode):

{
    "title": "Not Found",
    "status": 404,
    "detail": "The server has not found anything matching the Request-URI"
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoops, looks like something went wrong.

Why does it say "something" went wrong when it's clear what went "wrong" based on the status code?
Why does it say "wrong" when in theory I can return a successful error code from getStatusCode?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoops, looks like something went wrong.

Why does it say "something" went wrong when it's clear what went "wrong" based on the status code?

Because I want to be more friendly with an explicit and verbose error message. It's not arbitrary, it was taken from here https://github.com/symfony/symfony/blob/2.0/src/Symfony/Component/HttpKernel/Debug/ExceptionHandler.php#L77-L83 (since Symfony 2.0)

Why does it say "wrong" when in theory I can return a successful error code from getStatusCode?

Which wouldn't make sense to me.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The title already gives the text explanation of the status code. Anything else is just arbitrary and I don't see why you keep insisting on the detail property.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, revert these changes if you still think it's useless and arbitrary, but then I'll be agaisnt to have "detail" property for debug mode only, because (1) it is not intended for debugging information according to rfc; (2) for the same request consistently we must return the same response, at least this would apply to the members defined in the rfc, "detail" is one of them.

Anything else is considered an "extensions". In this case "exception" property (rendered on debug mode only) is an extension property.

It's problable that we're using the wrong names here and we should have called them "message" & "trace" respectively as extensions of the problem detail for debug mode only, so we can get rid of them on non-debug mode.

}

$content = [
'title' => $exception->getTitle(),
'status' => $exception->getStatusCode(),
'detail' => $message,
];
if ($debug) {
$content['detail'] = $exception->getMessage();
$content['exceptions'] = $exception->toArray();
}

Expand Down
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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