From dfb3f3f9ef04f352d382f7405a19aeb1eee3db6e Mon Sep 17 00:00:00 2001 From: David Grudl Date: Wed, 8 Nov 2023 18:48:38 +0100 Subject: [PATCH] exception handler can throw exception [Closes #307] --- src/Latte/Essential/Nodes/TryNode.php | 3 +-- src/Latte/Sandbox/Nodes/SandboxNode.php | 6 +++--- tests/sandbox/exceptionHandler.phpt | 7 +++++++ tests/tags/expected/general.n-attributes.php | 3 +-- tests/tags/expected/try.php | 3 +-- tests/tags/try.handler.phpt | 14 ++++++++++++++ 6 files changed, 27 insertions(+), 9 deletions(-) diff --git a/src/Latte/Essential/Nodes/TryNode.php b/src/Latte/Essential/Nodes/TryNode.php index f7d0f9e38..7379586ad 100644 --- a/src/Latte/Essential/Nodes/TryNode.php +++ b/src/Latte/Essential/Nodes/TryNode.php @@ -46,12 +46,11 @@ public function print(PrintContext $context): string try %line { %node } catch (Throwable $ʟ_e) { - ob_end_clean(); + ob_clean(); if (!($ʟ_e instanceof Latte\Essential\RollbackException) && isset($this->global->coreExceptionHandler)) { ($this->global->coreExceptionHandler)($ʟ_e, $this); } %node - ob_start(); } finally { echo ob_get_clean(); $iterator = $ʟ_it = $ʟ_try[%0.dump][0]; diff --git a/src/Latte/Sandbox/Nodes/SandboxNode.php b/src/Latte/Sandbox/Nodes/SandboxNode.php index 2ea744f60..c06558a8f 100644 --- a/src/Latte/Sandbox/Nodes/SandboxNode.php +++ b/src/Latte/Sandbox/Nodes/SandboxNode.php @@ -44,15 +44,15 @@ public function print(PrintContext $context): string ob_start(fn() => ''); try { $this->createTemplate(%node, %node, 'sandbox')->renderToContentType(%dump) %line; - echo ob_get_clean(); } catch (\Throwable $ʟ_e) { if (isset($this->global->coreExceptionHandler)) { - ob_end_clean(); + ob_clean(); ($this->global->coreExceptionHandler)($ʟ_e, $this); } else { - echo ob_get_clean(); throw $ʟ_e; } + } finally { + echo ob_get_clean(); } diff --git a/tests/sandbox/exceptionHandler.phpt b/tests/sandbox/exceptionHandler.phpt index a67764baa..4a720ec12 100644 --- a/tests/sandbox/exceptionHandler.phpt +++ b/tests/sandbox/exceptionHandler.phpt @@ -44,3 +44,10 @@ Assert::match( ); Assert::type(Latte\SecurityViolationException::class, $args[0]); Assert::type(Latte\Runtime\Template::class, $args[1]); + + +$latte->setExceptionHandler(fn(Throwable $e) => throw $e); +Assert::exception( + fn() => $latte->renderToString('main'), + Latte\SecurityViolationException::class, +); diff --git a/tests/tags/expected/general.n-attributes.php b/tests/tags/expected/general.n-attributes.php index 5a0f3785b..3f8f14f2b 100644 --- a/tests/tags/expected/general.n-attributes.php +++ b/tests/tags/expected/general.n-attributes.php @@ -323,11 +323,10 @@ public function main(array $ʟ_args): void echo "\n"; } catch (Throwable $ʟ_e) { - ob_end_clean(); + ob_clean(); if (!($ʟ_e instanceof Latte\Essential\RollbackException) && isset($this->global->coreExceptionHandler)) { ($this->global->coreExceptionHandler)($ʟ_e, $this); } - ob_start(); } finally { echo ob_get_clean(); $iterator = $ʟ_it = $ʟ_try[6][0]; diff --git a/tests/tags/expected/try.php b/tests/tags/expected/try.php index c65bb07e1..7b1e0f12b 100644 --- a/tests/tags/expected/try.php +++ b/tests/tags/expected/try.php @@ -10,14 +10,13 @@ '; } catch (Throwable $ʟ_e) { - ob_end_clean(); + ob_clean(); if (!($ʟ_e instanceof Latte\Essential\RollbackException) && isset($this->global->coreExceptionHandler)) { ($this->global->coreExceptionHandler)($ʟ_e, $this); } echo ' c '; - ob_start(); } finally { echo ob_get_clean(); $iterator = $ʟ_it = $ʟ_try[0][0]; diff --git a/tests/tags/try.handler.phpt b/tests/tags/try.handler.phpt index fe6052930..5dae6ac47 100644 --- a/tests/tags/try.handler.phpt +++ b/tests/tags/try.handler.phpt @@ -40,6 +40,20 @@ Assert::type(CustomException::class, $args[0]); Assert::type(Latte\Runtime\Template::class, $args[1]); +Assert::exception( + fn() => $latte->renderToString('{try}{=error()}{else}{=error()}{/try}'), + CustomException::class, +); + + $args = null; $latte->renderToString('{try}{rollback}{/try}'); Assert::null($args); + + +$latte->setExceptionHandler(fn(Throwable $e) => throw $e); + +Assert::exception( + fn() => $latte->renderToString('{try}{=error()}{/try}'), + CustomException::class, +);