bug #17434 Improved the error message when a template is not found (rvanginneken, javiereguiluz)

This PR was merged into the 2.3 branch.

Discussion
----------

Improved the error message when a template is not found

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | no
| Fixed tickets | #14806
| License       | MIT
| Doc PR        | -

### Before

![template_error_before](https://cloud.githubusercontent.com/assets/73419/12414237/7db29212-be94-11e5-85b4-c6444aa853f8.png)

### After

![template_error_after](https://cloud.githubusercontent.com/assets/73419/12414242/80ed1eca-be94-11e5-992e-a0596a1e95ca.png)

This seems to work in the browser ... but I can't make tests pass. Could anybody please help me? Thanks!

Commits
-------

0134d76 Simplified everything
19bfa2e Added a test
88b913b Fixed the problem in an easier way
35f082f Fixed a syntax issue
968476b Improved the error message when a template is not found
e9d951a [CodingStandards] Conformed to coding standards
d3fe07b [TwigBundle] fixed Include file locations in "Template could not be found" exception
This commit is contained in:
Fabien Potencier 2016-03-03 15:31:15 +01:00
commit 37569be45f
2 changed files with 19 additions and 3 deletions

View File

@ -77,19 +77,18 @@ class FilesystemLoader extends \Twig_Loader_Filesystem
try {
$file = parent::findTemplate($logicalName);
} catch (\Twig_Error_Loader $e) {
$previous = $e;
$twigLoaderException = $e;
// for BC
try {
$template = $this->parser->parse($template);
$file = $this->locator->locate($template);
} catch (\Exception $e) {
$previous = $e;
}
}
if (false === $file || null === $file) {
throw new \Twig_Error_Loader(sprintf('Unable to find template "%s".', $logicalName), -1, null, $previous);
throw $twigLoaderException;
}
return $this->cache[$logicalName] = $file;

View File

@ -98,4 +98,21 @@ class FilesystemLoaderTest extends TestCase
$loader = new FilesystemLoader($locator, $parser);
$loader->getCacheKey('name.format.engine');
}
/**
* @expectedException \Twig_Error_Loader
* @expectedExceptionMessageRegExp /Unable to find template "name\.format\.engine" \(looked into: .*\/Tests\/Loader\/\.\.\/DependencyInjection\/Fixtures\/Resources\/views\)/
*/
public function testTwigErrorIfTemplateDoesNotExist()
{
$parser = $this->getMock('Symfony\Component\Templating\TemplateNameParserInterface');
$locator = $this->getMock('Symfony\Component\Config\FileLocatorInterface');
$loader = new FilesystemLoader($locator, $parser);
$loader->addPath(__DIR__.'/../DependencyInjection/Fixtures/Resources/views');
$method = new \ReflectionMethod('Symfony\Bundle\TwigBundle\Loader\FilesystemLoader', 'findTemplate');
$method->setAccessible(true);
$method->invoke($loader, 'name.format.engine');
}
}