8000 feature #11313 [Config][Exception] Improve Routing Syntax Import Erro… · symfony/symfony@9d6c5d8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9d6c5d8

Browse files
committed
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
2 parents c6ef1bb + fec9a4a commit 9d6c5d8

File tree

2 files changed

+109
-7
lines changed

2 files changed

+109
-7
lines changed

src/Symfony/Component/Config/Exception/FileLoaderLoadException.php

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,39 @@ class FileLoaderLoadException extends \Exception
2626
*/
2727
public function __construct($resource, $sourceResource = null, $code = null, $previous = null)
2828
{
29-
if (null === $sourceResource) {
30-
$message = sprintf('Cannot load resource "%s".', $this->varToString($resource));
29+
$message = '';
30+
if ($previous) {
31+
// Include the previous exception, to help the user see what might be the underlying cause
32+
33+
// Trim the trailing period of the previous message. We only want 1 period remove so no rtrim...
34+
if ('.' === substr($previous->getMessage(), -1)) {
35+
$trimmedMessage = substr($previous->getMessage(), 0, -1);
36+
$message .= sprintf('%s', $trimmedMessage).' in ';
37+
} else {
38+
$message .= sprintf('%s', $previous->getMessage()).' in ';
39+
}
40+
$message .= $resource.' ';
41+
42+
// show tweaked trace to complete the human readable sentence
43+
if (null === $sourceResource) {
44+
$message .= sprintf('(which is loaded in resource "%s")', $this->varToString($resource));
45+
} else {
46+
$message .= sprintf('(which is being imported from "%s")',$this->varToString($sourceResource));
47+
}
48+
$message .= '.';
49+
50+
// if there's no previous message, present it the default way
51+
} elseif (null === $sourceResource) {
52+
$message .= sprintf('Cannot load resource "%s".', $this->varToString($resource));
3153
} else {
32-
$message = sprintf('Cannot import resource "%s" from "%s".', $this->varToString($resource), $this->varToString($sourceResource));
54+
$message .= sprintf('Cannot import resource "%s" from "%s".',$this->varToString($resource),$this->varToString($sourceResource));
3355
}
3456

3557
// Is the resource located inside a bundle?
3658
if ('@' === $resource[0]) {
3759
$parts = explode(DIRECTORY_SEPARATOR, $resource);
3860
$bundle = substr($parts[0], 1);
39-
$message .= ' '.sprintf('Make sure the "%s" bundle is correctly registered and loaded in the application kernel class.', $bundle);
40-
} elseif ($previous) {
41-
// include the previous exception, to help the user see what might be the underlying cause
42-
$message .= ' '.sprintf('(%s)', $previous->getMessage());
61+
$message .= ' '.sprintf('Make sure the "%s" bundle is correctly registered and loaded in the application kernel class.',$bundle);
4362
}
4463

4564
parent::__construct($message, $code, $previous);
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Config\Tests\Exception;
13+
14+
use Symfony\Component\Config\Exception\FileLoaderLoadException;
15+
16+
class FileLoaderLoadExceptionTest extends \PHPUnit_Framework_TestCase
17+
{
18+
public function testMessageCannotLoadResource()
19+
{
20+
$exception = new FileLoaderLoadException('resource', null);
21+
$this->assertEquals('Cannot load resource "resource".', $exception->getMessage());
22+
}
23+
24+
public function testMessageCannotImportResourceFromSource()
25+
{
26+
$exception = new FileLoaderLoadException('resource', 'sourceResource');
27+
$this->assertEquals('Cannot import resource "resource" from "sourceResource".', $exception->getMessage());
28+
}
29+
30+
public function testMessageCannotImportBundleResource()
31+
{
32+
$exception = new FileLoaderLoadException('@resource', 'sourceResource');
33+
$this->assertEquals(
34+
'Cannot import resource "@resource" from "sourceResource". ' .
35+
'Make sure the "resource" bundle is correctly registered and loaded in the application kernel class.',
36+
$exception->getMessage()
37+
);
38+
}
39+
40+
public function testMessageHasPreviousErrorWithDotAndUnableToLoad()
41+
{
42+
$exception = new FileLoaderLoadException(
43+
'resource',
44+
null,
45+
null,
46+
new \Exception('There was a previous error with an ending dot.')
47+
);
48+
$this->assertEquals(
49+
'There was a previous error with an ending dot in resource (which is loaded in resource "resource").',
50+
$exception->getMessage()
51+
);
52+
}
53+
54+
public function testMessageHasPreviousErrorWithoutDotAndUnableToLoad()
55+
{
56+
$exception = new FileLoaderLoadException(
57+
'resource',
58+
null,
59+
null,
60+
new \Exception('There was a previous error with no ending dot')
61+
);
62+
$this->assertEquals(
63+
'There was a previous error with no ending dot in resource (which is loaded in resource "resource").',
64+
$exception->getMessage()
65+
);
66+
}
67+
68+
public function testMessageHasPreviousErrorAndUnableToLoadBundle()
69+
{
70+
$exception = new FileLoaderLoadException(
71+
'@resource',
72+
null,
73+
null,
74+
new \Exception('There was a previous error with an ending dot.')
75+
);
76+
$this->assertEquals(
77+
'There was a previous error with an ending dot in @resource ' .
78+
'(which is loaded in resource "@resource"). ' .
79+
'Make sure the "resource" bundle is correctly registered and loaded in the application kernel class.',
80+
$exception->getMessage()
81+
);
82+
}
83+
}

0 commit comments

Comments
 (0)
0