Skip to content

Commit

Permalink
Fix Error when the trace has Twig file/line information instead of th…
Browse files Browse the repository at this point in the history
…e original PHP info
  • Loading branch information
fabpot committed Feb 26, 2025
1 parent 02118c7 commit 9e17bba
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/Error/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ class Error extends \Exception
private $lineno;
private $rawMessage;
private ?Source $source;
private string $phpFile;
private int $phpLine;

/**
* Constructor.
Expand All @@ -55,6 +57,8 @@ public function __construct(string $message, int $lineno = -1, ?Source $source =
{
parent::__construct('', 0, $previous);

$this->phpFile = $this->getFile();
$this->phpLine = $this->getLine();
$this->lineno = $lineno;
$this->source = $source;
$this->rawMessage = $message;
Expand Down Expand Up @@ -111,6 +115,8 @@ private function updateRepr(): void
$this->file = $this->source->getPath();
if ($this->lineno > 0) {
$this->line = $this->lineno;
} else {
$this->line = -1;
}
}

Expand Down Expand Up @@ -144,6 +150,8 @@ private function guessTemplateInfo(): void
if ($this->source->getName() === $trace['object']->getTemplateName() && !$isEmbedContainer) {
$template = $trace['object'];
$templateClass = \get_class($trace['object']);

break;
}
}
}
Expand All @@ -158,8 +166,7 @@ private function guessTemplateInfo(): void

while ($e = array_pop($exceptions)) {
$traces = $e->getTrace();
array_unshift($traces, ['file' => $e->getFile(), 'line' => $e->getLine()]);

array_unshift($traces, ['file' => $e instanceof Error ? $e->phpFile : $e->getFile(), 'line' => $e instanceof Error ? $e->phpLine : $e->getLine()]);
while ($trace = array_shift($traces)) {
if (!isset($trace['file']) || !isset($trace['line']) || $file != $trace['file']) {
continue;
Expand Down
51 changes: 51 additions & 0 deletions tests/ErrorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,18 @@
*/

use PHPUnit\Framework\TestCase;
use Twig\Attribute\YieldReady;
use Twig\Compiler;
use Twig\Environment;
use Twig\Error\Error;
use Twig\Error\RuntimeError;
use Twig\Error\SyntaxError;
use Twig\Loader\ArrayLoader;
use Twig\Loader\FilesystemLoader;
use Twig\Node\Node;
use Twig\Source;
use Twig\Token;
use Twig\TokenParser\AbstractTokenParser;

class ErrorTest extends TestCase
{
Expand Down Expand Up @@ -253,6 +258,52 @@ public function testTwigExceptionUpdateFileAndLineTogether()
}
}

public function testErrorWithoutLineAndContext()
{
$twig = new Environment(new ArrayLoader([
'index' => '{{ include("include") }}',
'include' => '{% foo %}',
]), ['debug' => true, 'cache' => false]);

$twig->addTokenParser(new class extends AbstractTokenParser {
public function parse(Token $token)
{
$this->parser->getStream()->expect(Token::BLOCK_END_TYPE);

return new #[YieldReady]class extends Node
{
public function __construct()
{
parent::__construct([], [], 2);
}

public function compile(Compiler $compiler): void
{
$compiler
->addDebugInfo($this)
->write('throw new \Twig\Error\RuntimeError("Runtime error.");')
;
}
};
}

public function getTag()
{
return 'foo';
}
});

try {
$twig->render('index');
$this->fail();
} catch (RuntimeError $e) {
$this->assertSame('Runtime error in "include" at line 2.', $e->getMessage());
$this->assertSame(2, $e->getTemplateLine());
$this->assertStringContainsString('Environment.php', $e->getFile());
$this->assertNotSame(2, $e->getLine());
}
}

public static function getErroredTemplates()
{
return [
Expand Down

0 comments on commit 9e17bba

Please sign in to comment.