diff --git a/lib/Twig/Environment.php b/lib/Twig/Environment.php index 48eaad397a..0b3c167c1c 100644 --- a/lib/Twig/Environment.php +++ b/lib/Twig/Environment.php @@ -438,6 +438,10 @@ public function createTemplate($template) } catch (Exception $e) { $this->setLoader($current); + throw $e; + } catch (Throwable $e) { + $this->setLoader($current); + throw $e; } $this->setLoader($current); diff --git a/lib/Twig/Node/Macro.php b/lib/Twig/Node/Macro.php index 932e795f0f..c186cd0b9f 100644 --- a/lib/Twig/Node/Macro.php +++ b/lib/Twig/Node/Macro.php @@ -109,6 +109,11 @@ public function compile(Twig_Compiler $compiler) ->write("ob_end_clean();\n\n") ->write("throw \$e;\n") ->outdent() + ->write("} catch (Throwable \$e) {\n") + ->indent() + ->write("ob_end_clean();\n\n") + ->write("throw \$e;\n") + ->outdent() ->write("}\n\n") ->write("return ('' === \$tmp = ob_get_clean()) ? '' : new Twig_Markup(\$tmp, \$this->env->getCharset());\n") ->outdent() diff --git a/lib/Twig/Template.php b/lib/Twig/Template.php index f0ce0fda14..7f7f62a52a 100644 --- a/lib/Twig/Template.php +++ b/lib/Twig/Template.php @@ -369,6 +369,12 @@ public function render(array $context) ob_end_clean(); } + throw $e; + } catch (Throwable $e) { + while (ob_get_level() > $level) { + ob_end_clean(); + } + throw $e; } diff --git a/test/Twig/Tests/Node/MacroTest.php b/test/Twig/Tests/Node/MacroTest.php index 901e57b91d..c7edfa2516 100644 --- a/test/Twig/Tests/Node/MacroTest.php +++ b/test/Twig/Tests/Node/MacroTest.php @@ -58,6 +58,10 @@ public function getfoo(\$__foo__ = null, \$__bar__ = "Foo"$declaration) } catch (Exception \$e) { ob_end_clean(); + throw \$e; + } catch (Throwable \$e) { + ob_end_clean(); + throw \$e; }