Skip to content

Commit

Permalink
Merge branch '3.x' into 4.x
Browse files Browse the repository at this point in the history
* 3.x:
  Improve error reporting
  • Loading branch information
fabpot committed Feb 22, 2025
2 parents c1fdaf1 + 7cde13f commit e31d709
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 25 deletions.
31 changes: 8 additions & 23 deletions src/Error/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,41 +106,26 @@ public function appendMessage($rawMessage): void

private function updateRepr(): void
{
$this->message = $this->rawMessage;

if ($this->source && $this->source->getPath() && $this->lineno > 0) {
$this->file = $this->source->getPath();
if ($this->lineno > 0) {
$this->line = $this->lineno;

return;
}

$dot = false;
if (str_ends_with($this->message, '.')) {
$this->message = substr($this->message, 0, -1);
$dot = true;
if ($this->source && $this->source->getPath()) {
$this->file = $this->source->getPath();
}

$questionMark = false;
if (str_ends_with($this->message, '?')) {
$this->message = $this->rawMessage;
$last = substr($this->message, -1);
if ($punctuation = '.' === $last || '?' === $last ? $last : '') {
$this->message = substr($this->message, 0, -1);
$questionMark = true;
}

if ($this->source && $this->source->getName()) {
$this->message .= \sprintf(' in "%s"', $this->source->getName());
}

if ($this->lineno > 0) {
$this->message .= \sprintf(' at line %d', $this->lineno);
}

if ($dot) {
$this->message .= '.';
}

if ($questionMark) {
$this->message .= '?';
if ($punctuation) {
$this->message .= $punctuation;
}
}

Expand Down
4 changes: 2 additions & 2 deletions tests/ErrorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public function testTwigExceptionGuessWithMissingVarAndFilesystemLoader()

$this->fail();
} catch (RuntimeError $e) {
$this->assertEquals('Variable "foo" does not exist.', $e->getMessage());
$this->assertEquals('Variable "foo" does not exist in "index.html" at line 3.', $e->getMessage());
$this->assertEquals(3, $e->getTemplateLine());
$this->assertEquals('index.html', $e->getSourceContext()->getName());
$this->assertEquals(3, $e->getLine());
Expand All @@ -117,7 +117,7 @@ public function testTwigExceptionGuessWithExceptionAndFilesystemLoader()

$this->fail();
} catch (RuntimeError $e) {
$this->assertEquals('An exception has been thrown during the rendering of a template ("Runtime error...").', $e->getMessage());
$this->assertEquals('An exception has been thrown during the rendering of a template ("Runtime error...") in "index.html" at line 3.', $e->getMessage());
$this->assertEquals(3, $e->getTemplateLine());
$this->assertEquals('index.html', $e->getSourceContext()->getName());
$this->assertEquals(3, $e->getLine());
Expand Down

0 comments on commit e31d709

Please sign in to comment.