ISSUE #11300: Improve Routing Syntax Import Error

modified FileLoaderLoadException message to present the previous (Parse)Exception first
This commit is contained in:
Jan Decavele 2014-07-05 12:53:25 +02:00 committed by Tom Van Looy
parent 8b54211471
commit 8ac5275751

View File

@ -26,20 +26,42 @@ class FileLoaderLoadException extends \Exception
*/
public function __construct($resource, $sourceResource = null, $code = null, $previous = null)
{
if (null === $sourceResource) {
$message = sprintf('Cannot load resource "%s".', $this->varToString($resource));
$message = '';
if ($previous) {
// include the previous exception, to help the user see what might be the underlying cause
//Trim the trailing period of the previous message. We only want 1 period remove so no rtrim...
if ('.' === substr($previous->getMessage(), -1)) {
$trimmedMessage = substr($previous->getMessage(), 0, -1);
$message .= ' ' . sprintf('%s', $trimmedMessage) . ' in ';
} else {
$message .= ' ' . sprintf('%s', $previous->getMessage()) . ' in ';
}
$message .= $resource . ' ';
// show tweaked trace to complete the human readable sentence
if (null === $sourceResource) {
$message .= sprintf('(which is loaded in resource "%s")', $this->varToString($resource));
} else {
$message .= sprintf(
'(which is being imported from "%s")',
$this->varToString($sourceResource)
);
}
$message .= '.';
// if there's no previous message, present it the default way
} elseif (null === $sourceResource) {
$message .= sprintf('Cannot load resource "%s".', $this->varToString($resource));
} else {
$message = sprintf('Cannot import resource "%s" from "%s".', $this->varToString($resource), $this->varToString($sourceResource));
$message .= sprintf('Cannot import resource "%s" from "%s".',$this->varToString($resource),$this->varToString($sourceResource));
}
// Is the resource located inside a bundle?
if ('@' === $resource[0]) {
$parts = explode(DIRECTORY_SEPARATOR, $resource);
$bundle = substr($parts[0], 1);
$message .= ' '.sprintf('Make sure the "%s" bundle is correctly registered and loaded in the application kernel class.', $bundle);
} elseif ($previous) {
// include the previous exception, to help the user see what might be the underlying cause
$message .= ' '.sprintf('(%s)', $previous->getMessage());
$message .= ' ' . sprintf('Make sure the "%s" bundle is correctly registered and loaded in the application kernel class.',$bundle);
}
parent::__construct($message, $code, $previous);