-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[HttpKernel][Debug] Get exception content according to request format #30851
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ | |
|
||
use Symfony\Component\Debug\Exception\FlattenException; | ||
use Symfony\Component\Debug\Exception\OutOfMemoryException; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\HttpKernel\Debug\FileLinkFormatter; | ||
|
||
/** | ||
|
@@ -189,6 +190,160 @@ public function sendPhpResponse($exception) | |
echo $this->decorate($this->getContent($exception), $this->getStylesheet($exception)); | ||
} | ||
|
||
/** | ||
* Gets the content associated with the given exception. | ||
* | ||
* @param \Exception|FlattenException $exception An \Exception or FlattenException instance | ||
* @param string $format The request format (html, json, xml, txt) | ||
* | ||
* @return string The formatted content as a string | ||
*/ | ||
public function getFormattedContent($exception, string $format): string | ||
{ | ||
switch ($format) { | ||
case 'json': | ||
return $this->getJson($exception); | ||
case 'xml': | ||
case 'rdf': | ||
return $this->getXml($exception); | ||
case 'txt': | ||
return $this->getTxt($exception); | ||
default: | ||
return $this->getHtml($exception); | ||
} | ||
} | ||
|
||
/** | ||
* Gets the JSON content associated with the given exception. | ||
* | ||
* @param \Exception|FlattenException $exception An \Exception or FlattenException instance | ||
* | ||
* @return string The JSON content as a string | ||
*/ | ||
public function getJson($exception): string | ||
{ | ||
if (!$exception instanceof FlattenException) { | ||
$exception = FlattenException::create($exception); | ||
} | ||
|
||
if (404 === $statusCode = $exception->getStatusCode()) { | ||
$title = 'Not Found'; | ||
} elseif (class_exists(Response::class) && isset(Response::$statusTexts[$statusCode])) { | ||
$title = Response::$statusTexts[$statusCode]; | ||
} else { | ||
$title = 'Internal Server Error'; | ||
} | ||
|
||
$content = [ | ||
'title' => $title, | ||
'status' => $statusCode, | ||
'detail' => $this->escapeHtml($exception->getMessage()), | ||
]; | ||
|
||
if ($this->debug) { | ||
$content['exceptions'] = $exception->toArray(); | ||
} | ||
|
||
return (string) json_encode($content); | ||
} | ||
|
||
/** | ||
* Gets the XML content associated with the given exception. | ||
* | ||
* @param \Exception|FlattenException $exception An \Exception or FlattenException instance | ||
* | ||
* @return string The XML content as a string | ||
*/ | ||
public function getXml($exception): string | ||
{ | ||
if (!$exception instanceof FlattenException) { | ||
$exception = FlattenException::create($exception); | ||
} | ||
|
||
if (404 === $statusCode = $exception->getStatusCode()) { | ||
$title = 'Not Found'; | ||
} elseif (class_exists(Response::class) && isset(Response::$statusTexts[$statusCode])) { | ||
$title = Response::$statusTexts[$statusCode]; | ||
} else { | ||
$title = 'Internal Server Error'; | ||
} | ||
$message = $this->escapeHtml($exception->getMessage()); | ||
|
||
$exceptions = ''; | ||
if ($this->debug) { | ||
$exceptions .= '<exceptions>'; | ||
foreach ($exception->toArray() as $e) { | ||
$exceptions .= sprintf('<exception class="%s" message="%s"><traces>', $e['class'], $this->escapeHtml($e['message'])); | ||
foreach ($e['trace'] as $trace) { | ||
$exceptions .= '<trace>'; | ||
if ($trace['function']) { | ||
$exceptions .= sprintf('at %s%s%s(%s) ', $trace['class'], $trace['type'], $trace['function'], strip_tags($this->formatArgs($trace['args']))); | ||
} | ||
if (isset($trace['file'], $trace['line'])) { | ||
$exceptions .= strip_tags($this->formatPath($trace['file'], $trace['line'])); | ||
} | ||
$exceptions .= '</trace>'; | ||
} | ||
$exceptions .= '</traces></exception>'; | ||
} | ||
$exceptions .= '</exceptions>'; | ||
} | ||
|
||
return <<<EOF | ||
<?xml version="1.0" encoding="{$this->charset}" ?> | ||
<problem xmlns="urn:ietf:rfc:7807"> | ||
<title>{$title}</title> | ||
<status>{$statusCode}</status> | ||
<detail>{$message}</detail> | ||
{$exceptions} | ||
</problem> | ||
EOF; | ||
} | ||
|
||
/** | ||
* Gets the TXT content associated with the given exception. | ||
* | ||
* @param \Exception|FlattenException $exception An \Exception or FlattenException instance | ||
* | ||
* @return string The TXT content as a string | ||
*/ | ||
public function getTxt($exception): string | ||
{ | ||
if (!$exception instanceof FlattenException) { | ||
$exception = FlattenException::create($exception); | ||
} | ||
|
||
if (404 === $statusCode = $exception->getStatusCode()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this blocks seems repeated for all content-types? maybe move it to a private method? |
||
$title = 'Not Found'; | ||
} elseif (class_exists(Response::class) && isset(Response::$statusTexts[$statusCode])) { | ||
$title = Response::$statusTexts[$statusCode]; | ||
} else { | ||
$title = 'Internal Server Error'; | ||
} | ||
|
||
$content = sprintf("[title] %s\n", $title); | ||
$content .= sprintf("[status] %s\n", $statusCode); | ||
$content .= sprintf("[detail] %s\n", $exception->getMessage()); | ||
|
||
if ($this->debug) { | ||
foreach ($exception->toArray() as $i => $e) { | ||
$content .= sprintf("[%d] %s: %s\n", $i + 1, $e['class'], $e['message']); | ||
|
||
foreach ($e['trace'] as $trace) { | ||
if ($trace['function']) { | ||
$content .= sprintf('at %s%s%s(%s) ', $trace['class'], $trace['type'], $trace['function'], strip_tags($this->formatArgs($trace['args']))); | ||
} | ||
if (isset($trace['file'], $trace['line'])) { | ||
$content .= strip_tags($this->formatPath($trace['file'], $trace['line'])); | ||
} | ||
$content .= "\n"; | ||
} | ||
} | ||
} | ||
|
||
return $content; | ||
} | ||
|
||
/** | ||
* Gets the full HTML content associated with the given exception. | ||
* | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
B41A
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so a 403 error without
symfony/http-foundation
installed would lead to this title/message? Might be a bit confusing 😄