Permalink
Browse files

bug #17434 Improved the error message when a template is not found (r…

…vanginneken, 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
  • Loading branch information...
fabpot committed Mar 3, 2016
2 parents eee9bfb + 0134d76 commit 37569be45f4da6e43177a9e6d1774899c5b6f336
@@ -77,19 +77,18 @@ protected function findTemplate($template, $throw = true)
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;
@@ -98,4 +98,21 @@ public function testTwigErrorIfLocatorReturnsFalse()
$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');
}
}

0 comments on commit 37569be

Please sign in to comment.