Skip to content

Commit

Permalink
Catch all errors on a call to Twig render and rethrow as Twig Errors.…
Browse files Browse the repository at this point in the history
… [Some Twig code was giving PHP errors in PHP8]
  • Loading branch information
trampgeek committed Jun 29, 2024
1 parent 7266ee7 commit d851c12
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions classes/twig.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,9 @@ private static function get_twig_environment($isstrict = false, $isdebug = false
// which is added the STUDENT parameter.
// Return the Twig-expanded string.
// Any Twig exceptions raised must be caught higher up.
// Since Twig range functions can result in PHP ValueError being thrown (grr)
// ValueErrors are caught and re-thrown as TwigErrors.
// Since Twig range functions can result in PHP ValueError being thrown, and
// a call to the slice filter with a string parameter gives an error.
// So all errors are caught and re-thrown as TwigErrors.
public static function render($s, $student, $parameters = [], $isstrict = false) {
if ($s === null || trim($s) === '') {
return '';
Expand All @@ -103,7 +104,7 @@ public static function render($s, $student, $parameters = [], $isstrict = false)
$template = $twig->createTemplate($s);
try {
$renderedstring = $template->render($parameters);
} catch (ValueError $e) {
} catch (Error $e) {
throw new \Twig\Error\Error("Twig error: " . $e->getMessage());
}
return $renderedstring;
Expand Down

0 comments on commit d851c12

Please sign in to comment.