-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[HttpKernel] Add better error me 8000 ssage when controller action isn't callable #10788
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -80,7 +80,7 @@ public function getController(Request $request) | |
$callable = $this->createController($co 10000 ntroller); | ||
|
||
if (!is_callable($callable)) { | ||
throw new \InvalidArgumentException(sprintf('Controller "%s" for URI "%s" is not callable.', $controller, $request->getPathInfo())); | ||
throw new \InvalidArgumentException(sprintf('The controller for URI "%s" is not callable. %s', $request->getPathInfo(), $this->getControllerError($callable))); | ||
} | ||
|
||
return $callable; | ||
|
@@ -167,4 +167,65 @@ protected function instantiateController($class) | |
{ | ||
return new $class(); | ||
} | ||
|
||
private function getControllerError($callable) | ||
{ | ||
if (is_string($callable)) { | ||
if (false !== strpos($callable, '::')) { | ||
$callable = explode('::', $callable); | ||
} | ||
|
||
if (class_exists($callable) && !method_exists($callable, '__invoke')) { | ||
return sprintf('Class "%s" does not have a method "__invoke".', $callable); | ||
} | ||
|
||
if (!function_exists($callable)) { | ||
return sprintf('Function "%s" does not exist.', $callable); | ||
} | ||
} | ||
|
||
if (!is_array($callable)) { | ||
return sprintf('Invalid type for controller given, expected string or array, got "%s".', gettype($callable)); | ||
} | ||
|
||
if (2 !== count($callable)) { | ||
return sprintf('Invalid format for controller, expected array(controller, method) or controller::method.'); | ||
} | ||
|
||
list($controller, $method) = $callable; | ||
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. you should handle the case where 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. otherwise it would break below btw 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. I think at this point all cases are handled. Even when passing crappy things like |
||
|
||
if (is_string($controller) && !class_exists($controller)) { | ||
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. we need an extension point for child classes extending the ControllerResolver and adding more supported case for strings (it can be a service id in FrameworkBundle, not only a class name) 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 function is only called when a controller is not callable. The process of getting the callable for the controller happens in the 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. but the message will tell you 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. It shouldn't, because the service id won't ever be passed to this function. When you make a typo, the error will come from ControllerResolver.php#L69 in the FrameworkBundle when the service doesn't exist 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. We need to improve the error message in this place though, otherwise they will still get a bad error message. 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 may over-complicate things, but what if createController was responsible for checking is_callable in different situations and throwing the clear exception there. If we want to prefix all the messages with "The controller for URI..." We could catch the exception in getController and prepend the message. |
||
return sprintf('Class "%s" does not exist.', $controller); | ||
} | ||
|
||
$className = is_object($controller) ? get_class($controller) : $controller; | ||
|
||
if (method_exists($controller, $method)) { | ||
return sprintf('Method "%s" on class "%s" should be public and non-abstract.', $method, $className); | ||
} | ||
|
||
$collection = get_class_methods($controller); | ||
|
||
$alternatives = array(); | ||
|
||
foreach ($collection as $item) { | ||
$lev = levenshtein($method, $item); | ||
|
||
if ($lev <= strlen($method) / 3 || false !== strpos($item, $method)) { | ||
$alternatives[] = $item; | ||
} | ||
} | ||
|
||
asort($alternatives); | ||
|
||
$message = sprintf('Expected method "%s" on class "%s"', $method, $className); | ||
|
||
67ED if (count($alternatives) > 0) { | ||
$message .= sprintf(', did you mean "%s"?', implode('", "', $alternatives)); | ||
} else { | ||
$message .= sprintf('. Available methods: "%s".', implode('", "', $collection)); | ||
} | ||
|
||
return $message; | ||
} | ||
} |
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.
The regular expression test is necessary because on travis when the tests are run using deps=low the tests failed because of the installed dependecies