From aeb9bffa8fa1ac288043d1c4b50947ff52cafb7a Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Tue, 11 Apr 2017 13:42:20 -0700 Subject: [PATCH] added a more specialized exception for a better error message --- .../FrameworkBundle/Routing/DelegatingLoader.php | 2 +- .../Config/Exception/FileLoaderLoadException.php | 10 +++++++++- .../Component/Config/Loader/DelegatingLoader.php | 2 +- src/Symfony/Component/Config/Loader/FileLoader.php | 2 +- src/Symfony/Component/Config/Loader/Loader.php | 2 +- .../Tests/Exception/FileLoaderLoadExceptionTest.php | 12 ++++++++++++ .../Component/Routing/RouteCollectionBuilder.php | 4 ++-- 7 files changed, 27 insertions(+), 7 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/Routing/DelegatingLoader.php b/src/Symfony/Bundle/FrameworkBundle/Routing/DelegatingLoader.php index 9783117f0b7f6..fce47e9eb8426 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Routing/DelegatingLoader.php +++ b/src/Symfony/Bundle/FrameworkBundle/Routing/DelegatingLoader.php @@ -64,7 +64,7 @@ public function load($resource, $type = null) // - this handles the case and prevents the second fatal error // by triggering an exception beforehand. - throw new FileLoaderLoadException($resource); + throw new FileLoaderLoadException($resource, null, null, null, $type); } $this->loading = true; diff --git a/src/Symfony/Component/Config/Exception/FileLoaderLoadException.php b/src/Symfony/Component/Config/Exception/FileLoaderLoadException.php index 6af3dd0a6d618..564f75ce60b8c 100644 --- a/src/Symfony/Component/Config/Exception/FileLoaderLoadException.php +++ b/src/Symfony/Component/Config/Exception/FileLoaderLoadException.php @@ -23,8 +23,9 @@ class FileLoaderLoadException extends \Exception * @param string $sourceResource The original resource importing the new resource * @param int $code The error code * @param \Exception $previous A previous exception + * @param string $type The type of resource */ - public function __construct($resource, $sourceResource = null, $code = null, $previous = null) + public function __construct($resource, $sourceResource = null, $code = null, $previous = null, $type = null) { $message = ''; if ($previous) { @@ -60,6 +61,13 @@ public function __construct($resource, $sourceResource = null, $code = null, $pr $bundle = substr($parts[0], 1); $message .= sprintf(' Make sure the "%s" bundle is correctly registered and loaded in the application kernel class.', $bundle); $message .= sprintf(' If the bundle is registered, make sure the bundle path "%s" is not empty.', $resource); + } elseif (null !== $type) { + // maybe there is no loader for this specific type + if ('annotation' === $type) { + $message .= ' Make sure annotations are enabled.'; + } else { + $message .= sprintf(' Make sure there is a loader supporting the "%s" type.', $type); + } } parent::__construct($message, $code, $previous); diff --git a/src/Symfony/Component/Config/Loader/DelegatingLoader.php b/src/Symfony/Component/Config/Loader/DelegatingLoader.php index 3097878bf0bf2..23b625652af2d 100644 --- a/src/Symfony/Component/Config/Loader/DelegatingLoader.php +++ b/src/Symfony/Component/Config/Loader/DelegatingLoader.php @@ -39,7 +39,7 @@ public function __construct(LoaderResolverInterface $resolver) public function load($resource, $type = null) { if (false === $loader = $this->resolver->resolve($resource, $type)) { - throw new FileLoaderLoadException($resource); + throw new FileLoaderLoadException($resource, null, null, null, $type); } return $loader->load($resource, $type); diff --git a/src/Symfony/Component/Config/Loader/FileLoader.php b/src/Symfony/Component/Config/Loader/FileLoader.php index 9f9bb11266581..d563bc026951c 100644 --- a/src/Symfony/Component/Config/Loader/FileLoader.php +++ b/src/Symfony/Component/Config/Loader/FileLoader.php @@ -210,7 +210,7 @@ private function doImport($resource, $type = null, $ignoreErrors = false, $sourc throw $e; } - throw new FileLoaderLoadException($resource, $sourceResource, null, $e); + throw new FileLoaderLoadException($resource, $sourceResource, null, $e, $type); } } } diff --git a/src/Symfony/Component/Config/Loader/Loader.php b/src/Symfony/Component/Config/Loader/Loader.php index a6f8d9c66454c..d2f2ec90b9b03 100644 --- a/src/Symfony/Component/Config/Loader/Loader.php +++ b/src/Symfony/Component/Config/Loader/Loader.php @@ -70,7 +70,7 @@ public function resolve($resource, $type = null) $loader = null === $this->resolver ? false : $this->resolver->resolve($resource, $type); if (false === $loader) { - throw new FileLoaderLoadException($resource); + throw new FileLoaderLoadException($resource, null, null, null, $type); } return $loader; diff --git a/src/Symfony/Component/Config/Tests/Exception/FileLoaderLoadExceptionTest.php b/src/Symfony/Component/Config/Tests/Exception/FileLoaderLoadExceptionTest.php index c1ad9150e6a99..7c5e167c44086 100644 --- a/src/Symfony/Component/Config/Tests/Exception/FileLoaderLoadExceptionTest.php +++ b/src/Symfony/Component/Config/Tests/Exception/FileLoaderLoadExceptionTest.php @@ -22,6 +22,18 @@ public function testMessageCannotLoadResource() $this->assertEquals('Cannot load resource "resource".', $exception->getMessage()); } + public function testMessageCannotLoadResourceWithType() + { + $exception = new FileLoaderLoadException('resource', null, null, null, 'foobar'); + $this->assertEquals('Cannot load resource "resource". Make sure there is a loader supporting the "foobar" type.', $exception->getMessage()); + } + + public function testMessageCannotLoadResourceWithAnnotationType() + { + $exception = new FileLoaderLoadException('resource', null, null, null, 'annotation'); + $this->assertEquals('Cannot load resource "resource". Make sure annotations are enabled.', $exception->getMessage()); + } + public function testMessageCannotImportResourceFromSource() { $exception = new FileLoaderLoadException('resource', 'sourceResource'); diff --git a/src/Symfony/Component/Routing/RouteCollectionBuilder.php b/src/Symfony/Component/Routing/RouteCollectionBuilder.php index 89b426266b84e..54bd86b7eaade 100644 --- a/src/Symfony/Component/Routing/RouteCollectionBuilder.php +++ b/src/Symfony/Component/Routing/RouteCollectionBuilder.php @@ -369,11 +369,11 @@ private function load($resource, $type = null) } if (null === $resolver = $this->loader->getResolver()) { - throw new FileLoaderLoadException($resource); + throw new FileLoaderLoadException($resource, null, null, null, $type); } if (false === $loader = $resolver->resolve($resource, $type)) { - throw new FileLoaderLoadException($resource); + throw new FileLoaderLoadException($resource, null, null, null, $type); } $collections = $loader->load($resource, $type);