Skip to content

Commit e31d709

Browse files
committed
Merge branch '3.x' into 4.x
* 3.x: Improve error reporting
2 parents c1fdaf1 + 7cde13f commit e31d709

File tree

2 files changed

+10
-25
lines changed

2 files changed

+10
-25
lines changed

src/Error/Error.php

+8-23
Original file line numberDiff line numberDiff line change
@@ -106,41 +106,26 @@ public function appendMessage($rawMessage): void
106106

107107
private function updateRepr(): void
108108
{
109-
$this->message = $this->rawMessage;
110-
111-
if ($this->source && $this->source->getPath() && $this->lineno > 0) {
112-
$this->file = $this->source->getPath();
109+
if ($this->lineno > 0) {
113110
$this->line = $this->lineno;
114-
115-
return;
116111
}
117-
118-
$dot = false;
119-
if (str_ends_with($this->message, '.')) {
120-
$this->message = substr($this->message, 0, -1);
121-
$dot = true;
112+
if ($this->source && $this->source->getPath()) {
113+
$this->file = $this->source->getPath();
122114
}
123115

124-
$questionMark = false;
125-
if (str_ends_with($this->message, '?')) {
116+
$this->message = $this->rawMessage;
117+
$last = substr($this->message, -1);
118+
if ($punctuation = '.' === $last || '?' === $last ? $last : '') {
126119
$this->message = substr($this->message, 0, -1);
127-
$questionMark = true;
128120
}
129-
130121
if ($this->source && $this->source->getName()) {
131122
$this->message .= \sprintf(' in "%s"', $this->source->getName());
132123
}
133-
134124
if ($this->lineno > 0) {
135125
$this->message .= \sprintf(' at line %d', $this->lineno);
136126
}
137-
138-
if ($dot) {
139-
$this->message .= '.';
140-
}
141-
142-
if ($questionMark) {
143-
$this->message .= '?';
127+
if ($punctuation) {
128+
$this->message .= $punctuation;
144129
}
145130
}
146131

tests/ErrorTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public function testTwigExceptionGuessWithMissingVarAndFilesystemLoader()
9898

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

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

0 commit comments

Comments
 (0)