Skip to content

Commit 37569be

Browse files
committed
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
2 parents eee9bfb + 0134d76 commit 37569be

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

src/Symfony/Bundle/TwigBundle/Loader/FilesystemLoader.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,19 +77,18 @@ protected function findTemplate($template, $throw = true)
7777
try {
7878
$file = parent::findTemplate($logicalName);
7979
} catch (\Twig_Error_Loader $e) {
80-
$previous = $e;
80+
$twigLoaderException = $e;
8181

8282
// for BC
8383
try {
8484
$template = $this->parser->parse($template);
8585
$file = $this->locator->locate($template);
8686
} catch (\Exception $e) {
87-
$previous = $e;
8887
}
8988
}
9089

9190
if (false === $file || null === $file) {
92-
throw new \Twig_Error_Loader(sprintf('Unable to find template "%s".', $logicalName), -1, null, $previous);
91+
throw $twigLoaderException;
9392
}
9493

9594
return $this->cache[$logicalName] = $file;

src/Symfony/Bundle/TwigBundle/Tests/Loader/FilesystemLoaderTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,21 @@ public function testTwigErrorIfLocatorReturnsFalse()
9898
$loader = new FilesystemLoader($locator, $parser);
9999
$loader->getCacheKey('name.format.engine');
100100
}
101+
102+
/**
103+
* @expectedException \Twig_Error_Loader
104+
* @expectedExceptionMessageRegExp /Unable to find template "name\.format\.engine" \(looked into: .*\/Tests\/Loader\/\.\.\/DependencyInjection\/Fixtures\/Resources\/views\)/
105+
*/
106+
public function testTwigErrorIfTemplateDoesNotExist()
107+
{
108+
$parser = $this->getMock('Symfony\Component\Templating\TemplateNameParserInterface');
109+
$locator = $this->getMock('Symfony\Component\Config\FileLocatorInterface');
110+
111+
$loader = new FilesystemLoader($locator, $parser);
112+
$loader->addPath(__DIR__.'/../DependencyInjection/Fixtures/Resources/views');
113+
114+
$method = new \ReflectionMethod('Symfony\Bundle\TwigBundle\Loader\FilesystemLoader', 'findTemplate');
115+
$method->setAccessible(true);
116+
$method->invoke($loader, 'name.format.engine');
117+
}
101118
}

0 commit comments

Comments
 (0)