From e44235760b49a9dc0af6305d09a4ea7b059d47b5 Mon Sep 17 00:00:00 2001 From: Christophe Coevoet Date: Wed, 6 Jun 2018 20:23:57 +0200 Subject: [PATCH] Report the proper location for errors compiled in templates The {% use %} and {% with %} tags are adding some runtime checks triggering exceptions in the compiled template. This ensures that they get the proper location. While the guessing was generally working fine for the {% with %} (and so this only makes the code faster), the guessing was not working for {% use %} due to the exception happening in the class constructor rather than on display (and so the guessing was finding the template which was triggering the load of the faulty template). --- lib/Twig/Node/Module.php | 10 +++++++--- lib/Twig/Node/With.php | 4 +++- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/lib/Twig/Node/Module.php b/lib/Twig/Node/Module.php index 55cb1a846..4cb6c195a 100644 --- a/lib/Twig/Node/Module.php +++ b/lib/Twig/Node/Module.php @@ -198,7 +198,9 @@ class Twig_Node_Module extends Twig_Node ->indent() ->write("throw new Twig_Error_Runtime('Template \"'.") ->subcompile($trait->getNode('template')) - ->raw(".'\" cannot be used as a trait.');\n") + ->raw(".'\" cannot be used as a trait.', ") + ->repr($node->getTemplateLine()) + ->raw(", \$this->source);\n") ->outdent() ->write("}\n") ->write(sprintf("\$_trait_%s_blocks = \$_trait_%s->getBlocks();\n\n", $i, $i)) @@ -210,11 +212,13 @@ class Twig_Node_Module extends Twig_Node ->string($key) ->raw("])) {\n") ->indent() - ->write("throw new Twig_Error_Runtime(sprintf('Block ") + ->write("throw new Twig_Error_Runtime('Block ") ->string($key) ->raw(' is not defined in trait ') ->subcompile($trait->getNode('template')) - ->raw(".'));\n") + ->raw(".', ") + ->repr($node->getTemplateLine()) + ->raw(", \$this->source);\n") ->outdent() ->write("}\n\n") diff --git a/lib/Twig/Node/With.php b/lib/Twig/Node/With.php index 2ab0ea5d7..baf721d0e 100644 --- a/lib/Twig/Node/With.php +++ b/lib/Twig/Node/With.php @@ -38,7 +38,9 @@ class Twig_Node_With extends Twig_Node ->raw(";\n") ->write(sprintf("if (!is_array(\$%s)) {\n", $varsName)) ->indent() - ->write("throw new Twig_Error_Runtime('Variables passed to the \"with\" tag must be a hash.');\n") + ->write("throw new Twig_Error_Runtime('Variables passed to the \"with\" tag must be a hash.', ") + ->repr($this->getTemplateLine()) + ->raw(", \$this->source);\n") ->outdent() ->write("}\n") ;