Skip to content

Commit 48b048d

Browse files
committed
feature symfony#22383 added a more specialized exception for a better error message (fabpot)
This PR was merged into the 3.3-dev branch. Discussion ---------- added a more specialized exception for a better error message | Q | A | ------------- | --- | Branch? | master | Bug fix? | yes/no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | n/a | License | MIT | Doc PR | n/a When trying to load a resource, it is very interesting to understand why a resource cannot be loaded. Especially when you get `Cannot load resource "../src/Controller/".`... when the real error is that annotation support is disabled. This PR adds more information in that case. Commits ------- aeb9bff added a more specialized exception for a better error message
2 parents 9fdd36f + aeb9bff commit 48b048d

File tree

7 files changed

+27
-7
lines changed

7 files changed

+27
-7
lines changed

src/Symfony/Bundle/FrameworkBundle/Routing/DelegatingLoader.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public function load($resource, $type = null)
6464
// - this handles the case and prevents the second fatal error
6565
// by triggering an exception beforehand.
6666

67-
throw new FileLoaderLoadException($resource);
67+
throw new FileLoaderLoadException($resource, null, null, null, $type);
6868
}
6969
$this->loading = true;
7070

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

+9-1
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ class FileLoaderLoadException extends \Exception
2323
* @param string $sourceResource The original resource importing the new resource
2424
* @param int $code The error code
2525
* @param \Exception $previous A previous exception
26+
* @param string $type The type of resource
2627
*/
27-
public function __construct($resource, $sourceResource = null, $code = null, $previous = null)
28+
public function __construct($resource, $sourceResource = null, $code = null, $previous = null, $type = null)
2829
{
2930
$message = '';
3031
if ($previous) {
@@ -60,6 +61,13 @@ public function __construct($resource, $sourceResource = null, $code = null, $pr
6061
$bundle = substr($parts[0], 1);
6162
$message .= sprintf(' Make sure the "%s" bundle is correctly registered and loaded in the application kernel class.', $bundle);
6263
$message .= sprintf(' If the bundle is registered, make sure the bundle path "%s" is not empty.', $resource);
64+
} elseif (null !== $type) {
65+
// maybe there is no loader for this specific type
66+
if ('annotation' === $type) {
67+
$message .= ' Make sure annotations are enabled.';
68+
} else {
69+
$message .= sprintf(' Make sure there is a loader supporting the "%s" type.', $type);
70+
}
6371
}
6472

6573
parent::__construct($message, $code, $previous);

src/Symfony/Component/Config/Loader/DelegatingLoader.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function __construct(LoaderResolverInterface $resolver)
3939
public function load($resource, $type = null)
4040
{
4141
if (false === $loader = $this->resolver->resolve($resource, $type)) {
42-
throw new FileLoaderLoadException($resource);
42+
throw new FileLoaderLoadException($resource, null, null, null, $type);
4343
}
4444

4545
return $loader->load($resource, $type);

src/Symfony/Component/Config/Loader/FileLoader.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ private function doImport($resource, $type = null, $ignoreErrors = false, $sourc
210210
throw $e;
211211
}
212212

213-
throw new FileLoaderLoadException($resource, $sourceResource, null, $e);
213+
throw new FileLoaderLoadException($resource, $sourceResource, null, $e, $type);
214214
}
215215
}
216216
}

src/Symfony/Component/Config/Loader/Loader.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function resolve($resource, $type = null)
7070
$loader = null === $this->resolver ? false : $this->resolver->resolve($resource, $type);
7171

7272
if (false === $loader) {
73-
throw new FileLoaderLoadException($resource);
73+
throw new FileLoaderLoadException($resource, null, null, null, $type);
7474
}
7575

7676
return $loader;

src/Symfony/Component/Config/Tests/Exception/FileLoaderLoadExceptionTest.php

+12
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,18 @@ public function testMessageCannotLoadResource()
2222
$this->assertEquals('Cannot load resource "resource".', $exception->getMessage());
2323
}
2424

25+
public function testMessageCannotLoadResourceWithType()
26+
{
27+
$exception = new FileLoaderLoadException('resource', null, null, null, 'foobar');
28+
$this->assertEquals('Cannot load resource "resource". Make sure there is a loader supporting the "foobar" type.', $exception->getMessage());
29+
}
30+
31+
public function testMessageCannotLoadResourceWithAnnotationType()
32+
{
33+
$exception = new FileLoaderLoadException('resource', null, null, null, 'annotation');
34+
$this->assertEquals('Cannot load resource "resource". Make sure annotations are enabled.', $exception->getMessage());
35+
}
36+
2537
public function testMessageCannotImportResourceFromSource()
2638
{
2739
$exception = new FileLoaderLoadException('resource', 'sourceResource');

src/Symfony/Component/Routing/RouteCollectionBuilder.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -369,11 +369,11 @@ private function load($resource, $type = null)
369369
}
370370

371371
if (null === $resolver = $this->loader->getResolver()) {
372-
throw new FileLoaderLoadException($resource);
372+
throw new FileLoaderLoadException($resource, null, null, null, $type);
373373
}
374374

375375
if (false === $loader = $resolver->resolve($resource, $type)) {
376-
throw new FileLoaderLoadException($resource);
376+
throw new FileLoaderLoadException($resource, null, null, null, $type);
377377
}
378378

379379
$collections = $loader->load($resource, $type);

0 commit comments

Comments
 (0)