Sync Error file and line

This commit is contained in:
Fabien Potencier
2025-02-26 13:11:07 +01:00
parent 7cde13f6c9
commit 4a121d90c6
2 changed files with 23 additions and 3 deletions
+4 -3
View File
@@ -106,11 +106,12 @@ class Error extends \Exception
private function updateRepr(): void
{
if ($this->lineno > 0) {
$this->line = $this->lineno;
}
if ($this->source && $this->source->getPath()) {
// we only update the file and the line together
$this->file = $this->source->getPath();
if ($this->lineno > 0) {
$this->line = $this->lineno;
}
}
$this->message = $this->rawMessage;
+19
View File
@@ -15,6 +15,7 @@ use PHPUnit\Framework\TestCase;
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\Source;
@@ -234,6 +235,24 @@ EOHTML,
}
}
public function testTwigExceptionUpdateFileAndLineTogether()
{
$twig = new Environment(new ArrayLoader([
'index' => "\n\n\n\n{{ foo() }}",
]), ['debug' => true, 'cache' => false]);
try {
$twig->load('index')->render([]);
} catch (SyntaxError $e) {
$this->assertSame('Unknown "foo" function in "index" at line 5.', $e->getMessage());
$this->assertSame(5, $e->getTemplateLine());
// as we are using an ArrayLoader, we don't have a file, so the line should not be the template line,
// but the line of the error in the Parser.php file
$this->assertStringContainsString('Parser.php', $e->getFile());
$this->assertNotSame(5, $e->getLine());
}
}
public static function getErroredTemplates()
{
return [