Skip to content

Improved an error message related to controllers #27499

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
Jun 27, 2018
Merged
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
22 changes: 15 additions & 7 deletions src/Symfony/Component/HttpKernel/HttpKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ private function handleRaw(Request $request, int $type = self::MASTER_REQUEST)
if ($event->hasResponse()) {
$response = $event->getResponse();
} else {
$msg = sprintf('The controller must return a response (%s given).', $this->varToString($response));
$msg = sprintf('The controller must return a "Symfony\Component\HttpFoundation\Response" object but it returned %s.', $this->varToString($response));

// the user may have forgotten to return something
if (null === $response) {
Expand Down Expand Up @@ -253,32 +253,40 @@ private function handleException(\Exception $e, Request $request, int $type): Re
private function varToString($var): string
{
if (is_object($var)) {
return sprintf('Object(%s)', get_class($var));
return sprintf('an object of type %s', get_class($var));
}

if (is_array($var)) {
$a = array();
foreach ($var as $k => $v) {
$a[] = sprintf('%s => %s', $k, $this->varToString($v));
$a[] = sprintf('%s => ...', $k);
}

return sprintf('Array(%s)', implode(', ', $a));
return sprintf('an array ([%s])', mb_substr(implode(', ', $a), 0, 255));
}

if (is_resource($var)) {
return sprintf('Resource(%s)', get_resource_type($var));
return sprintf('a resource (%s)', get_resource_type($var));
}

if (null === $var) {
return 'null';
}

if (false === $var) {
return 'false';
return 'a boolean value (false)';
}

if (true === $var) {
return 'true';
return 'a boolean value (true)';
}

if (is_string($var)) {
return sprintf('a string ("%s%s")', mb_substr($var, 0, 255), mb_strlen($var) > 255 ? '...' : '');
}

if (is_numeric($var)) {
return sprintf('a number (%s)', (string) $var);
}

return (string) $var;
Expand Down