File tree Expand file tree Collapse file tree 6 files changed +77
-20
lines changed
tests/Exception/UserException Expand file tree Collapse file tree 6 files changed +77
-20
lines changed Original file line number Diff line number Diff line change 44
55- Enh #150 : Cleanup templates, remove legacy code (@vjik )
66- New #151 : Add ` $traceLink ` parameter to ` HtmlRenderer ` to allow linking to trace files (@vjik )
7- - New #153 : Add ` UserExceptionInterface ` to mark user exceptions (@vjik )
7+ - New #153 : Introduce ` UserException ` attribute to mark user exceptions (@vjik )
88
99## 4.1.0 April 18, 2025
1010
Original file line number Diff line number Diff line change 44
55namespace Yiisoft \ErrorHandler \Exception ;
66
7+ use Attribute ;
78use Exception ;
9+ use ReflectionClass ;
10+ use Throwable ;
11+
12+ use function count ;
813
914/**
10- * `UserException` represents an exception that is meant to be shown to end users.
15+ * `UserException` is an exception anв class attribute that indicates
16+ * the exception message is safe to display to end users.
17+ *
18+ * Usage:
19+ * - throw directly (`throw new UserException(...)`) for explicit user-facing errors;
20+ * - annotate any exception class with the `#[UserException]` attribute
21+ * to mark its messages as user-facing without extending this class.
1122 *
1223 * @final
1324 */
14- class UserException extends Exception implements UserExceptionInterface
25+ #[Attribute(Attribute::TARGET_CLASS )]
26+ class UserException extends Exception
1527{
28+ public static function is (Throwable $ throwable ): bool
29+ {
30+ if ($ throwable instanceof self) {
31+ return true ;
32+ }
33+
34+ $ attributes = (new ReflectionClass ($ throwable ))->getAttributes (self ::class);
35+ return count ($ attributes ) > 0 ;
36+ }
1637}
Load Diff This file was deleted.
Original file line number Diff line number Diff line change 11<?php
22
3- use Yiisoft \ErrorHandler \Exception \UserExceptionInterface ;
3+ use Yiisoft \ErrorHandler \Exception \UserException ;
44use Yiisoft \ErrorHandler \Renderer \HtmlRenderer ;
55use Yiisoft \ErrorHandler \ThrowableRendererInterface ;
66
99 * @var HtmlRenderer $this
1010 */
1111
12- if ($ throwable instanceof UserExceptionInterface ) {
12+ if (UserException:: is ( $ throwable) ) {
1313 $ name = $ this ->getThrowableName ($ throwable );
1414 $ message = $ throwable ->getMessage ();
1515} else {
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ declare (strict_types=1 );
4+
5+ namespace Yiisoft \ErrorHandler \Tests \Exception \UserException ;
6+
7+ use Exception ;
8+ use Yiisoft \ErrorHandler \Exception \UserException ;
9+
10+ #[UserException]
11+ final class NotFoundException extends Exception
12+ {
13+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ declare (strict_types=1 );
4+
5+ namespace Yiisoft \ErrorHandler \Tests \Exception \UserException ;
6+
7+ use Exception ;
8+ use PHPUnit \Framework \Attributes \DataProvider ;
9+ use PHPUnit \Framework \TestCase ;
10+ use Throwable ;
11+ use Yiisoft \ErrorHandler \Exception \UserException ;
12+
13+ use function PHPUnit \Framework \assertInstanceOf ;
14+ use function PHPUnit \Framework \assertSame ;
15+
16+ final class UserExceptionTest extends TestCase
17+ {
18+ public function testUserExceptionInstance (): void
19+ {
20+ $ exception = new UserException ('User error message ' );
21+
22+ assertSame ('User error message ' , $ exception ->getMessage ());
23+ assertInstanceOf (Exception::class, $ exception );
24+ }
25+
26+ public static function dataIs (): iterable
27+ {
28+ yield [true , new UserException ()];
29+ yield [false , new Exception ()];
30+ yield [true , new NotFoundException ()];
31+ }
32+
33+ #[DataProvider('dataIs ' )]
34+ public function testIs (bool $ expected , Throwable $ exception ): void
35+ {
36+ assertSame ($ expected , UserException::is ($ exception ));
37+ }
38+ }
You can’t perform that action at this time.
0 commit comments