Handle non-UTF-8 and invalid JSON responses from WLED device#2052
Conversation
|
Warning Rate limit exceeded
You’ve run out of usage credits. Purchase more in the billing tab. ⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout. Please see our FAQ for further information. ℹ️ Review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (4)
✨ 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.25% +38.64%
===========================================
Files 6 8 +2
Lines 662 1094 +432
Branches 143 112 -31
===========================================
+ Hits 388 1064 +676
+ Misses 270 20 -250
- Partials 4 10 +6 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
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>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
81a5bbd to
142065c
Compare
There was a problem hiding this comment.
Pull request overview
This PR improves robustness of the WLED client when devices return corrupt responses by converting non-UTF-8 and invalid-JSON payload failures into a dedicated, more diagnostic exception (WLEDInvalidResponseError) rather than letting decode/parse errors propagate as unexpected exceptions.
Changes:
- Introduces
WLEDInvalidResponseErrorto represent invalid/corrupt responses from the device. - Catches
UnicodeDecodeErrorfromresponse.text()andorjson.JSONDecodeErrorfromorjson.loads()and raisesWLEDInvalidResponseErrorincluding{method} {uri}context. - Adds update() tests covering corrupt
/jsonand/presets.jsonresponses (invalid JSON and non-UTF-8).
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
src/wled/wled.py |
Adds explicit handling for non-UTF-8 and invalid JSON responses in the request pipeline. |
src/wled/exceptions.py |
Adds a new exception type WLEDInvalidResponseError. |
tests/test_wled.py |
Adds parametrized tests for corrupt /json and /presets.json update flows. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Wrap orjson.loads in 4xx/5xx JSON branch with try/except JSONDecodeError - Wrap non-UTF-8 decode in 4xx/5xx text branch to raise WLEDInvalidResponseError with method+URI instead of letting UnicodeDecodeError escape - Use `"application/json" in content_type` in error branch (consistent with success path) - Export WLEDInvalidResponseError from wled/__init__.py and __all__ - Assert exception message contains method+URI in corrupt-response tests - Add parametrized test for unparsable HTTP error bodies (non-UTF-8 and invalid JSON) 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