Skip to content

Commit d66a402

Browse files
committed
added a more specialized exception for a better error message
1 parent a841ffa commit d66a402

File tree

5 files changed

+24
-4
lines changed

5 files changed

+24
-4
lines changed

Exception/FileLoaderLoadException.php

Lines changed: 9 additions & 1 deletion
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);

Loader/DelegatingLoader.php

Lines changed: 1 addition & 1 deletion
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);

Loader/FileLoader.php

Lines changed: 1 addition & 1 deletion
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
}

Loader/Loader.php

Lines changed: 1 addition & 1 deletion
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;

Tests/Exception/FileLoaderLoadExceptionTest.php

Lines changed: 12 additions & 0 deletions
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');

0 commit comments

Comments
 (0)