From b1bffc0516bd245cc75bd2a971e50c08801e1702 Mon Sep 17 00:00:00 2001 From: James Hudson Date: Sat, 18 Oct 2014 15:59:01 +0100 Subject: [PATCH 1/2] Indicate which file was being parsed if an exception is thrown while running translation:debug When running the translation:debug command, if a template contains invalid twig markup, an exception is thrown. This patch rethrows a new exception that includes the filename being parsed in the message to aid debuging. --- src/Symfony/Bridge/Twig/Translation/TwigExtractor.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Bridge/Twig/Translation/TwigExtractor.php b/src/Symfony/Bridge/Twig/Translation/TwigExtractor.php index a7fc89954e..b387b1c1c1 100644 --- a/src/Symfony/Bridge/Twig/Translation/TwigExtractor.php +++ b/src/Symfony/Bridge/Twig/Translation/TwigExtractor.php @@ -58,7 +58,12 @@ class TwigExtractor implements ExtractorInterface $finder = new Finder(); $files = $finder->files()->name('*.twig')->in($directory); foreach ($files as $file) { - $this->extractTemplate(file_get_contents($file->getPathname()), $catalogue); + try { + $this->extractTemplate(file_get_contents($file->getPathname()), $catalogue); + } catch (\Twig_Error $e) { + $e->setTemplateFile($file->getPathname()); + throw $e; + } } } From 97a8f7e395ba59a52a4caaa36a6f2f12c34d4b52 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Sun, 2 Nov 2014 01:54:43 +0100 Subject: [PATCH 2/2] [TwigBundle] added a test --- .../Twig/Tests/Translation/TwigExtractorTest.php | 13 +++++++++++++ .../Bridge/Twig/Translation/TwigExtractor.php | 3 ++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Bridge/Twig/Tests/Translation/TwigExtractorTest.php b/src/Symfony/Bridge/Twig/Tests/Translation/TwigExtractorTest.php index a2c5cd3d03..d5614e7ec7 100644 --- a/src/Symfony/Bridge/Twig/Tests/Translation/TwigExtractorTest.php +++ b/src/Symfony/Bridge/Twig/Tests/Translation/TwigExtractorTest.php @@ -78,4 +78,17 @@ class TwigExtractorTest extends TestCase array('{{ "new key" | transchoice(domain="domain", count=1) }}', array('new key' => 'domain')), ); } + + /** + * @expectedException \Twig_Error + * @expectedExceptionMessage Unclosed "block" in "extractor/syntax_error.twig" at line 1 + */ + public function testExtractSyntaxError() + { + $twig = new \Twig_Environment(new \Twig_Loader_Array(array())); + $twig->addExtension(new TranslationExtension($this->getMock('Symfony\Component\Translation\TranslatorInterface'))); + + $extractor = new TwigExtractor($twig); + $extractor->extract(__DIR__.'/../Fixtures', new MessageCatalogue('en')); + } } diff --git a/src/Symfony/Bridge/Twig/Translation/TwigExtractor.php b/src/Symfony/Bridge/Twig/Translation/TwigExtractor.php index b387b1c1c1..fe36436c71 100644 --- a/src/Symfony/Bridge/Twig/Translation/TwigExtractor.php +++ b/src/Symfony/Bridge/Twig/Translation/TwigExtractor.php @@ -61,7 +61,8 @@ class TwigExtractor implements ExtractorInterface try { $this->extractTemplate(file_get_contents($file->getPathname()), $catalogue); } catch (\Twig_Error $e) { - $e->setTemplateFile($file->getPathname()); + $e->setTemplateFile($file->getRelativePathname()); + throw $e; } }