Skip to content

Commit

Permalink
exception handler can throw exception [Closes #307]
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Nov 8, 2023
1 parent 520178e commit dfb3f3f
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 9 deletions.
3 changes: 1 addition & 2 deletions src/Latte/Essential/Nodes/TryNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
6 changes: 3 additions & 3 deletions src/Latte/Sandbox/Nodes/SandboxNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}


Expand Down
7 changes: 7 additions & 0 deletions tests/sandbox/exceptionHandler.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
3 changes: 1 addition & 2 deletions tests/tags/expected/general.n-attributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
3 changes: 1 addition & 2 deletions tests/tags/expected/try.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
14 changes: 14 additions & 0 deletions tests/tags/try.handler.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);

0 comments on commit dfb3f3f

Please sign in to comment.