From 7510c3a335db795b7cbf185989bab4bbc629ec22 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Tue, 5 Jun 2018 08:43:23 +0200 Subject: [PATCH] Improved an error message related to controllers --- .../Component/HttpKernel/HttpKernel.php | 22 +++++++++++++------ 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/Symfony/Component/HttpKernel/HttpKernel.php b/src/Symfony/Component/HttpKernel/HttpKernel.php index 9ebf5847b4..eee1ca9830 100644 --- a/src/Symfony/Component/HttpKernel/HttpKernel.php +++ b/src/Symfony/Component/HttpKernel/HttpKernel.php @@ -157,7 +157,7 @@ class HttpKernel implements HttpKernelInterface, TerminableInterface 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) { @@ -253,20 +253,20 @@ class HttpKernel implements HttpKernelInterface, TerminableInterface 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) { @@ -274,11 +274,19 @@ class HttpKernel implements HttpKernelInterface, TerminableInterface } 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;