From 1cdc913f4ce063caff1242a2593af0de507ae41c Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Sun, 27 Oct 2013 14:47:59 +0100 Subject: [PATCH] fixed error filename/line when an error occurs in an included file To be able to cover all cases, we need to catch the exception when we know the execution context (the block being rendered). The only possibility is to actually try/catch exceptions in the displayBlock() method directly. --- CHANGELOG | 2 +- lib/Twig/Template.php | 22 +++++++++++++++++----- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index c0fa5d0..55e4b3c 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,6 @@ * 1.14.2 (2013-XX-XX) - * n/a + * fixed error filename/line when an error occurs in an included file * 1.14.1 (2013-10-15) diff --git a/lib/Twig/Template.php b/lib/Twig/Template.php index ba1d921..d1e281d 100644 --- a/lib/Twig/Template.php +++ b/lib/Twig/Template.php @@ -127,12 +127,24 @@ abstract class Twig_Template implements Twig_TemplateInterface { $name = (string) $name; + $template = null; if (isset($blocks[$name])) { - $b = $blocks; - unset($b[$name]); - call_user_func($blocks[$name], $context, $b); + $template = $blocks[$name][0]; + $block = $blocks[$name][1]; + unset($blocks[$name]); } elseif (isset($this->blocks[$name])) { - call_user_func($this->blocks[$name], $context, $blocks); + $template = $this->blocks[$name][0]; + $block = $this->blocks[$name][1]; + } + + if (null !== $template) { + try { + $template->$block($context, $blocks); + } catch (Twig_Error $e) { + throw $e; + } catch (Exception $e) { + throw new Twig_Error_Runtime(sprintf('An exception has been thrown during the rendering of a template ("%s").', $e->getMessage()), -1, $template->getTemplateName(), $e); + } } elseif (false !== $parent = $this->getParent($context)) { $parent->displayBlock($name, $context, array_merge($this->blocks, $blocks)); } @@ -276,7 +288,7 @@ abstract class Twig_Template implements Twig_TemplateInterface throw $e; } catch (Exception $e) { - throw new Twig_Error_Runtime(sprintf('An exception has been thrown during the rendering of a template ("%s").', $e->getMessage()), -1, null, $e); + throw new Twig_Error_Runtime(sprintf('An exception has been thrown during the rendering of a template ("%s").', $e->getMessage()), -1, $this->getTemplateName(), $e); } } -- 1.7.2.5