Skip to content

Commit a7a0e17

Browse files
committed
Allow Reader to display file path in its error messages
1 parent 8aef23a commit a7a0e17

2 files changed

Lines changed: 19 additions & 1 deletion

File tree

src/Phug/Reader.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22

33
namespace Phug;
44

5+
use Phug\Util\Partial\PathTrait;
56
use Phug\Util\SourceLocation;
67

78
/**
89
* A string reading utility that searches strings byte by byte.
910
*/
1011
class Reader
1112
{
13+
use PathTrait;
14+
1215
/**
1316
* An array of PREG errors with a good error message.
1417
*
@@ -910,12 +913,14 @@ protected function getPregErrorText($code = null)
910913
*/
911914
protected function throwException($message)
912915
{
916+
$path = $this->getPath();
913917
$exception = new ReaderException(
914918
new SourceLocation(null, $this->line, $this->offset),
915919
sprintf(
916-
"Failed to read: %s \nNear: %s \nLine: %s \nOffset: %s \nPosition: %s",
920+
"Failed to read: %s \nNear: %s%s\nLine: %s \nOffset: %s \nPosition: %s",
917921
$message,
918922
$this->peek(20),
923+
$path ? "\nFile: $path" : '',
919924
$this->line,
920925
$this->offset,
921926
$this->position

tests/Phug/ReaderTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,19 @@ public function testMatchFailsOnPregError()
243243
(new Reader('foobar foobar foobar'))->match('(?:\D+|<\d+>)*[!?]');
244244
}
245245

246+
/**
247+
* @covers ::throwException
248+
* @covers \Phug\ReaderException
249+
* @expectedException \Phug\ReaderException
250+
* @expectedExceptionMessage File: path.pug
251+
*/
252+
public function testPathInErrors()
253+
{
254+
$reader = new Reader('foobar foobar foobar');
255+
$reader->setPath('path.pug');
256+
$reader->match('(?:\D+|<\d+>)*[!?]');
257+
}
258+
246259
/**
247260
* @covers ::match
248261
* @covers ::getMatch

0 commit comments

Comments
 (0)