Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Use class name notation instead of strings for FriendlyException attr…
…ibute

- Remove unnecessary try/catch blocks as suggested in PR review
  • Loading branch information
shuangjie committed Apr 20, 2025
commit 9b259e2e11f79a7cb9c499916f759757172417d7
18 changes: 7 additions & 11 deletions src/Renderer/HtmlRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -507,18 +507,14 @@ public function getThrowableName(Throwable $throwable): string
$name = $throwable->getName() . ' (' . $name . ')';
} else {
// Check if the exception class has FriendlyException attribute
try {
$reflectionClass = new ReflectionClass($throwable);
if (class_exists('Yiisoft\FriendlyException\Attribute\FriendlyException')) {
$attributes = $reflectionClass->getAttributes('Yiisoft\FriendlyException\Attribute\FriendlyException');

if (!empty($attributes)) {
$friendlyExceptionAttribute = $attributes[0]->newInstance();
$name = $friendlyExceptionAttribute->name . ' (' . $name . ')';
}
$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 . ')';
}
} catch (\Throwable $e) {
// Ignore exception and keep default name
}
}

Expand Down
8 changes: 4 additions & 4 deletions templates/development.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
$solution = $isFriendlyException ? $throwable->getSolution() : null;

// Check if the exception class has FriendlyException attribute
if ($solution === null && class_exists('Yiisoft\FriendlyException\Attribute\FriendlyException')) {
if ($solution === null && class_exists(\Yiisoft\FriendlyException\Attribute\FriendlyException::class)) {
try {
$reflectionClass = new ReflectionClass($throwable);
$attributes = $reflectionClass->getAttributes('Yiisoft\FriendlyException\Attribute\FriendlyException');
$attributes = $reflectionClass->getAttributes(\Yiisoft\FriendlyException\Attribute\FriendlyException::class);

if (!empty($attributes)) {
$friendlyExceptionAttribute = $attributes[0]->newInstance();
Expand Down Expand Up @@ -111,10 +111,10 @@
// Check if the exception class has FriendlyException attribute
$hasFriendlyNameFromAttribute = false;

if (class_exists('Yiisoft\FriendlyException\Attribute\FriendlyException')) {
if (class_exists(\Yiisoft\FriendlyException\Attribute\FriendlyException::class)) {
try {
$reflectionClass = new ReflectionClass($throwable);
$attributes = $reflectionClass->getAttributes('Yiisoft\FriendlyException\Attribute\FriendlyException');
$attributes = $reflectionClass->getAttributes(\Yiisoft\FriendlyException\Attribute\FriendlyException::class);

if (!empty($attributes)) {
$friendlyExceptionAttribute = $attributes[0]->newInstance();
Expand Down