bug #2700 Report the proper location for errors compiled in templates (stof)

This PR was merged into the 2.x branch.

Discussion
----------

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).

Commits
-------

e4423576 Report the proper location for errors compiled in templates
This commit is contained in:
Fabien Potencier
2018-06-07 07:42:53 +02:00
2 changed files with 10 additions and 4 deletions
+7 -3
View File
@@ -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")
+3 -1
View File
@@ -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")
;