Skip to content

Commit

Permalink
Make NotFoundException fiendly
Browse files Browse the repository at this point in the history
  • Loading branch information
np25071984 committed Jan 13, 2025
1 parent 3447fdc commit bb56b6f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/NotFoundException.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
use Exception;
use Psr\Container\NotFoundExceptionInterface;
use Throwable;
use Yiisoft\FriendlyException\FriendlyExceptionInterface;

/**
* `NotFoundException` is thrown when no definition or class was found in the container for a given ID.
*/
final class NotFoundException extends Exception implements NotFoundExceptionInterface
final class NotFoundException extends Exception implements NotFoundExceptionInterface, FriendlyExceptionInterface
{
/**
* @param string $id ID of the definition or name of the class that was not found.
Expand Down Expand Up @@ -49,4 +50,20 @@ public function getBuildStack(): array
{
return $this->buildStack;
}

public function getName(): string
{
return sprintf('No difinition or class found for "%s" ID.', $this->id);
}

public function getSolution(): ?string
{
$solution = <<<SOLUTION
Ensure that either a service with ID "%1\$s" is defined or such class exists and is autoloadable.
Ensure that configuration for service with ID "%1\$s" is correct.
SOLUTION;

return sprintf($solution, $this->id);
}
}
9 changes: 9 additions & 0 deletions tests/Unit/NotFoundExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ public function testGetId(): void
$exception = new NotFoundException('test');

$this->assertSame('test', $exception->getId());
$this->assertSame('No difinition or class found for "test" ID.', $exception->getName());
$this->assertSame(
<<<SOLUTION
Ensure that either a service with ID "test" is defined or such class exists and is autoloadable.
Ensure that configuration for service with ID "test" is correct.
SOLUTION,
$exception->getSolution()
);
}

public function testMessage(): void
Expand Down

0 comments on commit bb56b6f

Please sign in to comment.