Fix HtmlRenderer to handle FriendlyException attribute safely
| Q | A |
|---|---|
| Is bugfix? | ❌ |
| New feature? | ✓ |
| Breaks BC? | ❌ |
| Fixed issues | Issue #16 |
This PR fixes two issues in HtmlRenderer:
- Fixed a TypeError in
htmlEncode()function by properly converting Throwable object to string before encoding - Added safe handling for the
FriendlyExceptionattribute when the class is not available
These 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 givenError: Attribute class "Yiisoft\FriendlyException\Attribute\FriendlyException" not found
This implementation adds proper type checking and class existence verification to prevent errors.
Codecov Report
Attention: Patch coverage is 42.85714% with 4 lines in your changes missing coverage. Please review.
Project coverage is 80.32%. Comparing base (
44f49fa) to head (40f70ae).
| Files with missing lines | Patch % | Lines |
|---|---|---|
| src/Renderer/HtmlRenderer.php | 42.85% | 4 Missing :warning: |
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
:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.
:rocket: New features to boost your workflow:
- :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
What is Yiisoft\FriendlyException\Attribute\FriendlyException ?
What is
Yiisoft\FriendlyException\Attribute\FriendlyException?
This class is from a potential future implementation of PHP 8 attributes in the friendly-exception package, related to Issue #16 (https://github.com/yiisoft/friendly-exception/issues/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.