Handle non-UTF-8 and invalid JSON responses from WLED device#2052
Handle non-UTF-8 and invalid JSON responses from WLED device#2052mik-laj wants to merge 7 commits intofrenck:mainfrom
Conversation
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Raises WLEDInvalidResponseError instead of crashing with an unhandled UnicodeDecodeError or JSONDecodeError when the device returns corrupt data (e.g. binary garbage in presets.json). Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
|
Important Review skippedDraft detected. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ Finishing Touches🧪 Generate unit tests (beta)
Tip 💬 Introducing Slack Agent: The best way for teams to turn conversations into code.Slack Agent is built on CodeRabbit's deep understanding of your code, so your team can collaborate across the entire SDLC without losing context.
Built for teams:
One agent for your entire SDLC. Right inside Slack. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #2052 +/- ##
===========================================
+ Coverage 58.61% 97.23% +38.62%
===========================================
Files 6 8 +2
Lines 662 1084 +422
Branches 143 112 -31
===========================================
+ Hits 388 1054 +666
+ Misses 270 20 -250
- Partials 4 10 +6 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Proposed Changes
Some WLED devices can return corrupt data that causes an unhandled exception in the integration, silently presenting the user with a generic "Failed to connect" or "An unknown error occurred" message with no diagnostic information in the logs.
Two concrete failure modes observed in the wild ( home-assistant/core#162199
):
presets.json— the device returns binary garbage (e.g. bytes starting with\xff\xfe), which causesresponse.text()to raiseUnicodeDecodeErrorbefore any JSON parsing takes place.Content-Type: application/jsonbut the body is not valid JSON, causingorjson.loads()to raiseJSONDecodeError.Both exceptions were previously unhandled and propagated as unexpected errors. This change catches them explicitly and raises
WLEDInvalidResponseError(a new exception class) with a message that includes the HTTP method and URI, making it straightforward to identify the problematic endpoint in logs.Changes:
WLEDInvalidResponseError(WLEDError)toexceptions.pyresponse.text()in atry/except UnicodeDecodeErrororjson.loads()in atry/except orjson.JSONDecodeError/jsonand/presets.json× non-UTF-8 and invalid JSON)References: home-assistant/core#162199
Related Issues