feature #11313 [Config][Exception] Improve Routing Syntax Import Error (Jan Decavele, tvlooy)

This PR was merged into the 2.6-dev branch.

Discussion
----------

[Config][Exception] Improve Routing Syntax Import Error

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #11300
| License       | MIT
| Doc PR        | n/a

Commits
-------

fec9a4a removed some more spaces
16134d9 Merge remote-tracking branch 'upstream/master'
b099936 - Removed spaces around the concation dots to be more consitent - adjusted some formatting
0459d89 Addition of the symfony license text
de43182 Add test and small code fix
8ac5275 ISSUE #11300: Improve Routing Syntax Import Error
This commit is contained in:
Fabien Potencier 2014-08-28 09:05:14 +02:00
commit 9d6c5d807d
2 changed files with 109 additions and 7 deletions

View File

@ -26,20 +26,39 @@ 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('Cannot import resource "%s" from "%s".', $this->varToString($resource), $this->varToString($sourceResource));
$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));
}
// 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);

View File

@ -0,0 +1,83 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Config\Tests\Exception;
use Symfony\Component\Config\Exception\FileLoaderLoadException;
class FileLoaderLoadExceptionTest extends \PHPUnit_Framework_TestCase
{
public function testMessageCannotLoadResource()
{
$exception = new FileLoaderLoadException('resource', null);
$this->assertEquals('Cannot load resource "resource".', $exception->getMessage());
}
public function testMessageCannotImportResourceFromSource()
{
$exception = new FileLoaderLoadException('resource', 'sourceResource');
$this->assertEquals('Cannot import resource "resource" from "sourceResource".', $exception->getMessage());
}
public function testMessageCannotImportBundleResource()
{
$exception = new FileLoaderLoadException('@resource', 'sourceResource');
$this->assertEquals(
'Cannot import resource "@resource" from "sourceResource". ' .
'Make sure the "resource" bundle is correctly registered and loaded in the application kernel class.',
$exception->getMessage()
);
}
public function testMessageHasPreviousErrorWithDotAndUnableToLoad()
{
$exception = new FileLoaderLoadException(
'resource',
null,
null,
new \Exception('There was a previous error with an ending dot.')
);
$this->assertEquals(
'There was a previous error with an ending dot in resource (which is loaded in resource "resource").',
$exception->getMessage()
);
}
public function testMessageHasPreviousErrorWithoutDotAndUnableToLoad()
{
$exception = new FileLoaderLoadException(
'resource',
null,
null,
new \Exception('There was a previous error with no ending dot')
);
$this->assertEquals(
'There was a previous error with no ending dot in resource (which is loaded in resource "resource").',
$exception->getMessage()
);
}
public function testMessageHasPreviousErrorAndUnableToLoadBundle()
{
$exception = new FileLoaderLoadException(
'@resource',
null,
null,
new \Exception('There was a previous error with an ending dot.')
);
$this->assertEquals(
'There was a previous error with an ending dot in @resource ' .
'(which is loaded in resource "@resource"). ' .
'Make sure the "resource" bundle is correctly registered and loaded in the application kernel class.',
$exception->getMessage()
);
}
}