Skip to content

Handle non-UTF-8 and invalid JSON responses from WLED device#2052

Draft
mik-laj wants to merge 7 commits intofrenck:mainfrom
mik-laj:fix/handle-invalid-response
Draft

Handle non-UTF-8 and invalid JSON responses from WLED device#2052
mik-laj wants to merge 7 commits intofrenck:mainfrom
mik-laj:fix/handle-invalid-response

Conversation

@mik-laj
Copy link
Copy Markdown
Collaborator

@mik-laj mik-laj commented May 4, 2026

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
):

  1. Corrupt presets.json — the device returns binary garbage (e.g. bytes starting with \xff\xfe), which causes response.text() to raise UnicodeDecodeError before any JSON parsing takes place.
  2. Invalid JSON — the device returns a response with Content-Type: application/json but the body is not valid JSON, causing orjson.loads() to raise JSONDecodeError.

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:

  • Add WLEDInvalidResponseError(WLEDError) to exceptions.py
  • Wrap response.text() in a try/except UnicodeDecodeError
  • Wrap orjson.loads() in a try/except orjson.JSONDecodeError
  • Add tests for all four new error paths (/json and /presets.json × non-UTF-8 and invalid JSON)

References: home-assistant/core#162199

Related Issues

mik-laj and others added 6 commits April 30, 2026 03:46
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>
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 4, 2026

Important

Review skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: a1c2642e-ddb8-4789-aa0d-31120db697e6

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

  • Generate code and open pull requests
  • Plan features and break down work
  • Investigate incidents and troubleshoot customer tickets together
  • Automate recurring tasks and respond to alerts with triggers
  • Summarize progress and report instantly

Built for teams:

  • Shared memory across your entire org—no repeating context
  • Per-thread sandboxes to safely plan and execute work
  • Governance built-in—scoped access, auditability, and budget controls

One agent for your entire SDLC. Right inside Slack.

👉 Get started


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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@mik-laj mik-laj added the new-feature New features or options. label May 4, 2026
@codecov
Copy link
Copy Markdown

codecov Bot commented May 4, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.23%. Comparing base (3e87d76) to head (81a5bbd).
⚠️ Report is 597 commits behind head on main.

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

new-feature New features or options.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant