Skip to content

Commit

Permalink
Add option for full control over FileNotFoundException message
Browse files Browse the repository at this point in the history
  • Loading branch information
caendesilva committed Oct 27, 2023
1 parent 6a413a8 commit 39503b5
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ class FileNotFoundException extends Exception
/** @var int */
protected $code = 404;

public function __construct(?string $path = null)
public function __construct(?string $path = null, ?string $customMessage = null)
{
parent::__construct($path ? sprintf('File [%s] not found.', Hyde::pathToRelative($path)) : $this->message);
parent::__construct($customMessage ?? ($path ? sprintf('File [%s] not found.', Hyde::pathToRelative($path)) : $this->message));
}
}
5 changes: 5 additions & 0 deletions packages/framework/tests/Unit/CustomExceptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ public function testFileNotFoundExceptionWithAbsolutePath()
$this->assertSame('File [foo] not found.', (new FileNotFoundException(Hyde::path('foo')))->getMessage());
}

public function testFileNotFoundExceptionWithCustomPath()
{
$this->assertSame('foo', (new FileNotFoundException(customMessage: 'foo'))->getMessage());
}

public function testRouteNotFoundExceptionWithDefaultMessage()
{
$this->assertSame('Route not found.', (new RouteNotFoundException())->getMessage());
Expand Down

0 comments on commit 39503b5

Please sign in to comment.