error-handler icon indicating copy to clipboard operation
error-handler copied to clipboard

Fix HtmlRenderer to handle FriendlyException attribute safely

Open shuangjie opened this issue 8 months ago • 4 comments

Q A
Is bugfix?
New feature?
Breaks BC?
Fixed issues Issue #16

This PR fixes two issues in HtmlRenderer:

  1. Fixed a TypeError in htmlEncode() function by properly converting Throwable object to string before encoding
  2. Added safe handling for the FriendlyException attribute 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 given
  • Error: Attribute class "Yiisoft\FriendlyException\Attribute\FriendlyException" not found

This implementation adds proper type checking and class existence verification to prevent errors.

shuangjie avatar Apr 17 '25 10:04 shuangjie

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.

codecov[bot] avatar Apr 17 '25 10:04 codecov[bot]

What is Yiisoft\FriendlyException\Attribute\FriendlyException ?

vjik avatar Apr 18 '25 05:04 vjik

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.

shuangjie avatar Apr 19 '25 16:04 shuangjie

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.

samdark avatar Apr 19 '25 19:04 samdark