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

Skip to content

Commit 0252a00

Browse files
committed
feature #27499 Improved an error message related to controllers (javiereguiluz)
This PR was squashed before being merged into the 4.2-dev branch (closes #27499). Discussion ---------- Improved an error message related to controllers | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - This proposal is irrelevant for experienced users but it may be useful for newcomers. After having delivered several introductory Symfony training, I can say that when someone adds `return "some string...";` in their controller, the error message is confusing: ![before](https://user-images.githubusercontent.com/73419/40959468-0faf790a-689d-11e8-9ce1-f6e0caf4b113.png) Maybe we can reword it a bit? (I'm open for suggestions to improve the error message) ![after](https://user-images.githubusercontent.com/73419/40959505-29747070-689d-11e8-834e-92bf18760469.png) Commits ------- 7510c3a Improved an error message related to controllers
2 parents 1a3d445 + 7510c3a commit 0252a00

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

src/Symfony/Component/HttpKernel/HttpKernel.php

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ private function handleRaw(Request $request, int $type = self::MASTER_REQUEST)
157157
if ($event->hasResponse()) {
158158
$response = $event->getResponse();
159159
} else {
160-
$msg = sprintf('The controller must return a response (%s given).', $this->varToString($response));
160+
$msg = sprintf('The controller must return a "Symfony\Component\HttpFoundation\Response" object but it returned %s.', $this->varToString($response));
161161

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

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

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

268268
if (is_resource($var)) {
269-
return sprintf('Resource(%s)', get_resource_type($var));
269+
return sprintf('a resource (%s)', get_resource_type($var));
270270
}
271271

272272
if (null === $var) {
273273
return 'null';
274274
}
275275

276276
if (false === $var) {
277-
return 'false';
277+
return 'a boolean value (false)';
278278
}
279279

280280
if (true === $var) {
281-
return 'true';
281+
return 'a boolean value (true)';
282+
}
283+
284+
if (is_string($var)) {
285+
return sprintf('a string ("%s%s")', mb_substr($var, 0, 255), mb_strlen($var) > 255 ? '...' : '');
286+
}
287+
288+
if (is_numeric($var)) {
289+
return sprintf('a number (%s)', (string) $var);
282290
}
283291

284292
return (string) $var;

0 commit comments

Comments
 (0)