-
-
Notifications
You must be signed in to change notification settings - Fork 15
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?
Conversation
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #147 +/- ##
============================================
- Coverage 80.55% 80.32% -0.24%
- Complexity 210 213 +3
============================================
Files 19 19
Lines 679 686 +7
============================================
+ Hits 547 551 +4
- Misses 132 135 +3 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
What is |
This class is from a potential future implementation of PHP 8 attributes in the friendly-exception package, related to Issue #16 (yiisoft/friendly-exception#16). Currently, this class doesn't exist in the official friendly-exception package. My PR adds defensive coding to ensure error-handler works correctly both with current versions (where this class doesn't exist) and with future versions (if/when PHP 8 attributes are implemented). I created test files that use this attribute to verify the defensive coding works properly. I've marked these tests as skipped when the attribute class is not available. If you prefer, I can remove the attribute-related test files and only keep the safety checks for the htmlEncode method, which fixes a real issue with TypeError when encoding exception objects. |
I like the concept. It allows marking expceptions as friendly without extending the interface and is a good addition to what we already have. That's of course, works for static text solutions only but in many cases these are enough. |
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.
Let's extract getting FE names to a class and reuse it?
src/Renderer/HtmlRenderer.php
Outdated
// Check if the exception class has FriendlyException attribute | ||
try { | ||
$reflectionClass = new ReflectionClass($throwable); | ||
if (class_exists('Yiisoft\FriendlyException\Attribute\FriendlyException')) { |
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
if (class_exists('Yiisoft\FriendlyException\Attribute\FriendlyException')) { | |
if (class_exists(\Yiisoft\FriendlyException\Attribute\FriendlyException::class)) { |
src/Renderer/HtmlRenderer.php
Outdated
try { | ||
$reflectionClass = new ReflectionClass($throwable); | ||
if (class_exists('Yiisoft\FriendlyException\Attribute\FriendlyException')) { | ||
$attributes = $reflectionClass->getAttributes('Yiisoft\FriendlyException\Attribute\FriendlyException'); |
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.
Same
src/Renderer/HtmlRenderer.php
Outdated
$name = $friendlyExceptionAttribute->name . ' (' . $name . ')'; | ||
} | ||
} | ||
} catch (\Throwable) { |
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.
Where do you expect an exception? I'd remove try/catch
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.
You're right, the try/catch block isn't necessary here. When checking for the FriendlyException attribute, it would be better to let any exceptions propagate normally, making potential issues easier to discover. I'll remove the try/catch block. Thank you for the suggestion!
templates/development.php
Outdated
@@ -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')) { |
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.
Same
…ibute - Remove unnecessary try/catch blocks as suggested in PR review
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.
done
This PR fixes two issues in HtmlRenderer:
htmlEncode()
function by properly converting Throwable object to string before encodingFriendlyException
attribute when the class is not availableThese changes allow the error handler to work properly with different versions of the friendly-exception package, making it more robust.
The changes are needed because some tests were failing with:
TypeError: HtmlRenderer::htmlEncode(): Argument #1 ($content) must be of type string, RuntimeException given
Error: Attribute class "Yiisoft\FriendlyException\Attribute\FriendlyException" not found
This implementation adds proper type checking and class existence verification to prevent errors.