generated from yiisoft/package-template
-
-
Notifications
You must be signed in to change notification settings - Fork 17
Introduce UserException attribute
#153
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7ec9e5c
Add `UserExceptionInterface`
vjik 42d78d5
fix
vjik 58f1837
Refactor to attribute
vjik 98ab936
Update src/Exception/UserException.php
vjik bbb6a71
Update src/Exception/UserException.php
vjik 07b3ba3
rename method
vjik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,14 +4,34 @@ | |
|
|
||
| namespace Yiisoft\ErrorHandler\Exception; | ||
|
|
||
| use Attribute; | ||
| use Exception; | ||
| use ReflectionClass; | ||
| use Throwable; | ||
|
|
||
| use function count; | ||
|
|
||
| /** | ||
| * UserException is the base class for exceptions that are meant to be shown to end users. | ||
| * Such exceptions are often caused by mistakes of end users. | ||
| * `UserException` is an exception and class attribute that indicates | ||
| * the exception message is safe to display to end users. | ||
| * | ||
| * Usage: | ||
| * - throw directly (`throw new UserException(...)`) for explicit user-facing errors; | ||
| * - annotate any exception class with the `#[UserException]` attribute | ||
| * to mark its messages as user-facing without extending this class. | ||
| * | ||
| * @final | ||
| */ | ||
| #[Attribute(Attribute::TARGET_CLASS)] | ||
| class UserException extends Exception | ||
| { | ||
| public static function is(Throwable $throwable): bool | ||
samdark marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| if ($throwable instanceof self) { | ||
| return true; | ||
| } | ||
|
|
||
| $attributes = (new ReflectionClass($throwable))->getAttributes(self::class); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can caching affect performance? e.g long running apps
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it doesn't make sense in the case of exception handling. |
||
| return count($attributes) > 0; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Yiisoft\ErrorHandler\Tests\Exception\UserException; | ||
|
|
||
| use Exception; | ||
| use Yiisoft\ErrorHandler\Exception\UserException; | ||
|
|
||
| #[UserException] | ||
| final class NotFoundException extends Exception | ||
| { | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Yiisoft\ErrorHandler\Tests\Exception\UserException; | ||
|
|
||
| use Exception; | ||
| use PHPUnit\Framework\Attributes\DataProvider; | ||
| use PHPUnit\Framework\TestCase; | ||
| use Throwable; | ||
| use Yiisoft\ErrorHandler\Exception\UserException; | ||
|
|
||
| use function PHPUnit\Framework\assertInstanceOf; | ||
| use function PHPUnit\Framework\assertSame; | ||
|
|
||
| final class UserExceptionTest extends TestCase | ||
| { | ||
| public function testUserExceptionInstance(): void | ||
| { | ||
| $exception = new UserException('User error message'); | ||
|
|
||
| assertSame('User error message', $exception->getMessage()); | ||
| assertInstanceOf(Exception::class, $exception); | ||
| } | ||
|
|
||
| public static function dataIs(): iterable | ||
| { | ||
| yield [true, new UserException()]; | ||
| yield [false, new Exception()]; | ||
| yield [true, new NotFoundException()]; | ||
| } | ||
|
|
||
| #[DataProvider('dataIs')] | ||
| public function testIs(bool $expected, Throwable $exception): void | ||
| { | ||
| assertSame($expected, UserException::is($exception)); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.