Skip to content

added a more specialized exception for a better error message #22383

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Config/Loader/DelegatingLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Config/Loader/FileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Config/Loader/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/Routing/RouteCollectionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down