From d851c12199933fddc0d30d75875b2e7144ede7e3 Mon Sep 17 00:00:00 2001 From: Richard Lobb Date: Sun, 30 Jun 2024 11:35:17 +1200 Subject: [PATCH] Catch all errors on a call to Twig render and rethrow as Twig Errors. [Some Twig code was giving PHP errors in PHP8] --- classes/twig.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/classes/twig.php b/classes/twig.php index 73659843a..5bc58b8e7 100644 --- a/classes/twig.php +++ b/classes/twig.php @@ -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 ''; @@ -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;