diff --git a/src/Renderer/HtmlRenderer.php b/src/Renderer/HtmlRenderer.php index c85acb0..a09d1ff 100644 --- a/src/Renderer/HtmlRenderer.php +++ b/src/Renderer/HtmlRenderer.php @@ -14,7 +14,9 @@ use Yiisoft\ErrorHandler\Exception\ErrorException; use Yiisoft\ErrorHandler\ThrowableRendererInterface; use Yiisoft\FriendlyException\FriendlyExceptionInterface; +use Yiisoft\FriendlyException\Attribute\FriendlyException; use Yiisoft\Http\Header; +use ReflectionClass; use function array_values; use function dirname; @@ -491,9 +493,11 @@ public function createServerInformationLink(ServerRequestInterface $request): st } /** - * Returns the name of the throwable instance. + * Returns string representation of the throwable name. * - * @return string The name of the throwable instance. + * @param Throwable $throwable The throwable. + * + * @return string The throwable name. */ public function getThrowableName(Throwable $throwable): string { @@ -501,6 +505,17 @@ public function getThrowableName(Throwable $throwable): string if ($throwable instanceof FriendlyExceptionInterface) { $name = $throwable->getName() . ' (' . $name . ')'; + } else { + // Check if the exception class has FriendlyException attribute + $reflectionClass = new ReflectionClass($throwable); + if (class_exists(\Yiisoft\FriendlyException\Attribute\FriendlyException::class)) { + $attributes = $reflectionClass->getAttributes(\Yiisoft\FriendlyException\Attribute\FriendlyException::class); + + if (!empty($attributes)) { + $friendlyExceptionAttribute = $attributes[0]->newInstance(); + $name = $friendlyExceptionAttribute->name . ' (' . $name . ')'; + } + } } return $name; diff --git a/templates/development.php b/templates/development.php index be386b3..a06760e 100644 --- a/templates/development.php +++ b/templates/development.php @@ -1,8 +1,13 @@ getSolution() : null; + +// Check if the exception class has FriendlyException attribute +if ($solution === null && class_exists(\Yiisoft\FriendlyException\Attribute\FriendlyException::class)) { + try { + $reflectionClass = new ReflectionClass($throwable); + $attributes = $reflectionClass->getAttributes(\Yiisoft\FriendlyException\Attribute\FriendlyException::class); + + if (!empty($attributes)) { + $friendlyExceptionAttribute = $attributes[0]->newInstance(); + $solution = $friendlyExceptionAttribute->solution; + } + } catch (\Throwable $e) { + // Ignore exception + } +} + $exceptionClass = get_class($throwable); $exceptionMessage = $throwable->getMessage(); @@ -78,13 +99,44 @@
+ $hasFriendlyName = false; + if ($isFriendlyException) { + $hasFriendlyName = true; + ?> htmlEncode($throwable->getName())?> - - - + getAttributes(\Yiisoft\FriendlyException\Attribute\FriendlyException::class); + + if (!empty($attributes)) { + $friendlyExceptionAttribute = $attributes[0]->newInstance(); + $hasFriendlyNameFromAttribute = true; + ?> + htmlEncode($friendlyExceptionAttribute->name) ?> + — + + + + (Code #getCode() ?>)
@@ -98,12 +150,12 @@ renderPreviousExceptions($originalException) ?> - + Copied! diff --git a/tests/Renderer/Attribute/HtmlRendererWithAttributeTest.php b/tests/Renderer/Attribute/HtmlRendererWithAttributeTest.php new file mode 100644 index 0000000..7c6c170 --- /dev/null +++ b/tests/Renderer/Attribute/HtmlRendererWithAttributeTest.php @@ -0,0 +1,25 @@ +markTestSkipped('The attribute feature is not available in the current version of friendly-exception package'); + + $renderer = new HtmlRenderer(); + $exception = new TestExceptionWithAttribute(); + + $name = $renderer->getThrowableName($exception); + + $this->assertStringContainsString('Test Exception Name', $name); + $this->assertStringContainsString($exception::class, $name); + } +} \ No newline at end of file diff --git a/tests/Support/TestExceptionWithAttribute.php b/tests/Support/TestExceptionWithAttribute.php new file mode 100644 index 0000000..c154bed --- /dev/null +++ b/tests/Support/TestExceptionWithAttribute.php @@ -0,0 +1,17 @@ +