Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use friendly exception #386

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- New #380: Add `TagReference::id()` method (@vjik)
- Enh #384: Make `$config` parameter in `Container` constructor optional (@np25071984)
- Enh #324: Make `BuildingException` friendly (@np25071984)

## 1.3.0 October 14, 2024

Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
"php": "^8.1",
"ext-mbstring": "*",
"psr/container": "^1.1|^2.0",
"yiisoft/definitions": "^3.0"
"yiisoft/definitions": "^3.0",
"yiisoft/friendly-exception": "^1.1.0"
},
"require-dev": {
"league/container": "^4.2",
Expand Down
15 changes: 14 additions & 1 deletion src/BuildingException.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
use Exception;
use Psr\Container\ContainerExceptionInterface;
use Throwable;
use Yiisoft\FriendlyException\FriendlyExceptionInterface;

/**
* It wraps all exceptions that don't implement `ContainerExceptionInterface` during the build process.
* Also adds building context for more understanding.
*/
final class BuildingException extends Exception implements ContainerExceptionInterface
final class BuildingException extends Exception implements ContainerExceptionInterface, FriendlyExceptionInterface
{
/**
* @param string $id ID of the definition or name of the class that wasn't found.
Expand All @@ -32,4 +33,16 @@ public function __construct(

parent::__construct($message, 0, $previous);
}

public function getName(): string
{
return 'Couldn\'t build requested object.';
np25071984 marked this conversation as resolved.
Show resolved Hide resolved
}

public function getSolution(): ?string
{
return <<<SOLUTION
See (https://github.com/yiisoft/di)[https://github.com/yiisoft/di] for more documentation.
vjik marked this conversation as resolved.
Show resolved Hide resolved
SOLUTION;
}
}
2 changes: 2 additions & 0 deletions tests/Unit/BuildingExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public function testMessage(): void
$exception = new BuildingException('test', new RuntimeException('i am angry'));

$this->assertSame('Caught unhandled error "i am angry" while building "test".', $exception->getMessage());
$this->assertSame('Couldn\'t build requested object.', $exception->getName());
$this->assertSame('See (https://github.com/yiisoft/di)[https://github.com/yiisoft/di] for more documentation.', $exception->getSolution());
}

public function testEmptyMessage(): void
Expand Down
Loading