-
-
Notifications
You must be signed in to change notification settings - Fork 17
Fix HtmlRenderer to handle FriendlyException attribute safely #147
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
base: master
Are you sure you want to change the base?
Changes from 2 commits
c08cf44
40f70ae
9b259e2
c67a10b
2f7bad2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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,16 +493,33 @@ | |
} | ||
|
||
/** | ||
* 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 | ||
{ | ||
$name = $throwable::class; | ||
|
||
if ($throwable instanceof FriendlyExceptionInterface) { | ||
$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'); | ||
Check failure on line 513 in src/Renderer/HtmlRenderer.php
|
||
|
||
|
||
if (!empty($attributes)) { | ||
$friendlyExceptionAttribute = $attributes[0]->newInstance(); | ||
$name = $friendlyExceptionAttribute->name . ' (' . $name . ')'; | ||
} | ||
} | ||
} catch (\Throwable) { | ||
|
||
// Ignore exception and keep default name | ||
} | ||
} | ||
|
||
return $name; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Psr\Http\Message\ServerRequestInterface; | ||
use ReflectionClass; | ||
use Throwable; | ||
use Yiisoft\ErrorHandler\CompositeException; | ||
use Yiisoft\ErrorHandler\Exception\ErrorException; | ||
use Yiisoft\ErrorHandler\HeadersProvider; | ||
use Yiisoft\ErrorHandler\Renderer\HtmlRenderer; | ||
use Yiisoft\FriendlyException\FriendlyExceptionInterface; | ||
|
||
|
@@ -20,6 +25,22 @@ | |
} | ||
$isFriendlyException = $throwable instanceof FriendlyExceptionInterface; | ||
$solution = $isFriendlyException ? $throwable->getSolution() : null; | ||
|
||
// Check if the exception class has FriendlyException attribute | ||
if ($solution === null && class_exists('Yiisoft\FriendlyException\Attribute\FriendlyException')) { | ||
|
||
try { | ||
$reflectionClass = new ReflectionClass($throwable); | ||
$attributes = $reflectionClass->getAttributes('Yiisoft\FriendlyException\Attribute\FriendlyException'); | ||
|
||
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 @@ | |
<div class="exception-card"> | ||
<div class="exception-class"> | ||
<?php | ||
if ($isFriendlyException): ?> | ||
$hasFriendlyName = false; | ||
if ($isFriendlyException) { | ||
$hasFriendlyName = true; | ||
?> | ||
<span><?= $this->htmlEncode($throwable->getName())?></span> | ||
— | ||
<?= $exceptionClass ?> | ||
<?php else: ?> | ||
<span><?= $exceptionClass ?></span> | ||
<?php endif ?> | ||
<?php | ||
} else { | ||
// Check if the exception class has FriendlyException attribute | ||
$hasFriendlyNameFromAttribute = false; | ||
|
||
if (class_exists('Yiisoft\FriendlyException\Attribute\FriendlyException')) { | ||
shuangjie marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
try { | ||
$reflectionClass = new ReflectionClass($throwable); | ||
$attributes = $reflectionClass->getAttributes('Yiisoft\FriendlyException\Attribute\FriendlyException'); | ||
|
||
if (!empty($attributes)) { | ||
$friendlyExceptionAttribute = $attributes[0]->newInstance(); | ||
$hasFriendlyNameFromAttribute = true; | ||
?> | ||
<span><?= $this->htmlEncode($friendlyExceptionAttribute->name) ?></span> | ||
— | ||
<?= $exceptionClass ?> | ||
<?php | ||
} | ||
} catch (\Throwable $e) { | ||
// Ignore exception | ||
} | ||
} | ||
|
||
if (!$hasFriendlyName && !$hasFriendlyNameFromAttribute) { | ||
?> | ||
<span><?= $exceptionClass ?></span> | ||
<?php | ||
} | ||
} | ||
?> | ||
(Code #<?= $throwable->getCode() ?>) | ||
</div> | ||
|
||
|
@@ -98,12 +150,12 @@ | |
|
||
<?= $this->renderPreviousExceptions($originalException) ?> | ||
|
||
<textarea id="clipboard"><?= $this->htmlEncode($throwable) ?></textarea> | ||
<textarea id="clipboard"><?= $this->htmlEncode((string)$throwable) ?></textarea> | ||
<span id="copied">Copied!</span> | ||
|
||
<a href="#" | ||
class="copy-clipboard" | ||
data-clipboard="<?= $this->htmlEncode($throwable) ?>" | ||
data-clipboard="<?= $this->htmlEncode((string)$throwable) ?>" | ||
title="Copy the stacktrace for use in a bug report or pastebin" | ||
> | ||
<svg width="26" height="30" fill="none" xmlns="http://www.w3.org/2000/svg"> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\ErrorHandler\Tests\Renderer\Attribute; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Yiisoft\ErrorHandler\Renderer\HtmlRenderer; | ||
use Yiisoft\ErrorHandler\Tests\Support\TestExceptionWithAttribute; | ||
|
||
final class HtmlRendererWithAttributeTest extends TestCase | ||
{ | ||
public function testGetThrowableNameWithAttribute(): void | ||
{ | ||
$this->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); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\ErrorHandler\Tests\Support; | ||
|
||
use Exception; | ||
use Yiisoft\FriendlyException\Attribute\FriendlyException; | ||
|
||
#[FriendlyException(name: 'Test Exception Name', solution: 'This is a test solution for an exception.')] | ||
final class TestExceptionWithAttribute extends Exception | ||
{ | ||
public function __construct() | ||
{ | ||
parent::__construct('This is a test exception with attribute.'); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use full class name notation without quotes