Skip to content

Commit

Permalink
bug twigphp#3830 Catch errors thrown during template rendering (richa…
Browse files Browse the repository at this point in the history
…rds-square)

This PR was squashed before being merged into the 3.x branch.

Discussion
----------

Catch errors thrown during template rendering

Some errors, like not providing a function the proper number of arguments or division by zero, extend from `\Error` rather than `\Exception`. This PR catches these types of errors during template rendering and throws a `RuntimeError` in order to provide better debugging information.

Commits
-------

85bf01b Catch errors thrown during template rendering
  • Loading branch information
fabpot committed Oct 27, 2023
2 parents 9a602c7 + 85bf01b commit b285fcc
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/Error/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class Error extends \Exception
* @param int $lineno The template line where the error occurred
* @param Source|null $source The source context where the error occurred
*/
public function __construct(string $message, int $lineno = -1, Source $source = null, \Exception $previous = null)
public function __construct(string $message, int $lineno = -1, Source $source = null, \Throwable $previous = null)
{
parent::__construct('', 0, $previous);

Expand Down
4 changes: 2 additions & 2 deletions src/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ public function displayBlock($name, array $context, array $blocks = [], $useBloc
}

throw $e;
} catch (\Exception $e) {
} catch (\Throwable $e) {
$e = new RuntimeError(sprintf('An exception has been thrown during the rendering of a template ("%s").', $e->getMessage()), -1, $template->getSourceContext(), $e);
$e->guess();

Expand Down Expand Up @@ -404,7 +404,7 @@ protected function displayWithErrorHandling(array $context, array $blocks = [])
}

throw $e;
} catch (\Exception $e) {
} catch (\Throwable $e) {
$e = new RuntimeError(sprintf('An exception has been thrown during the rendering of a template ("%s").', $e->getMessage()), -1, $this->getSourceContext(), $e);
$e->guess();

Expand Down
22 changes: 22 additions & 0 deletions tests/ErrorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,28 @@ public function testTwigArrayReduceThrowsRuntimeExceptions()
}
}

public function testTwigArgumentCountErrorThrowsRuntimeExceptions()
{
$loader = new ArrayLoader([
'argument-error.html' => <<<EOHTML
{# max requires at least one argument #}
{{ max() }}
EOHTML
]);

$twig = new Environment($loader, ['debug' => true, 'cache' => false]);

$template = $twig->load('argument-error.html');
try {
$template->render();

$this->fail();
} catch (RuntimeError $e) {
$this->assertEquals(2, $e->getTemplateLine());
$this->assertEquals('argument-error.html', $e->getSourceContext()->getName());
}
}

public function getErroredTemplates()
{
return [
Expand Down

0 comments on commit b285fcc

Please sign in to comment.