Skip to content

Raise a RadioBrowserError for responses that cannot be parsed - #1566

Merged
frenck merged 1 commit into
mainfrom
frenck/wrap-parse-errors
Oct 1, 2026
Merged

frenck merged 1 commit into
mainfrom
frenck/wrap-parse-errors

Conversation

@frenck

@frenck frenck commented Oct 1, 2026

Copy link
Copy Markdown
Owner

Breaking change

None. Calls that used to raise a raw parser exception on a broken response now raise a RadioBrowserError instead, which is what the library documents.

Proposed change

Found in a review of the library. Every public method parsed the response outside of the error handling in _request(), so a response the library could not make sense of leaked a raw exception:

stats() with body '{'            -> orjson.JSONDecodeError
station_click() with body '{}'   -> KeyError: 'url'
search() with body '[{}]'        -> mashumaro.exceptions.MissingField

The README and AGENTS.md promise that everything that goes wrong while talking to the API raises a RadioBrowserError, and Home Assistant relies on that: it catches RadioBrowserError, so one broken response from one of the community servers became an unhandled error there.

A small unexpected_response() context manager now wraps the parsing in every public method, and turns parser and model errors (AttributeError, LookupError, TypeError, ValueError) into a RadioBrowserError with the original exception as its cause. It only wraps the parsing, so a real bug elsewhere is not hidden. It is deliberately not a connection error, so it is not retried: asking again usually returns the same broken response.

The new test runs all eight public methods against six kinds of broken bodies; all 48 cases fail without this change.

Type of change

  • Dependency or tooling upgrade
  • Bugfix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Deprecation (replaces or removes a feature, with a migration path)
  • Breaking change (a fix or feature that changes existing behavior)
  • Code quality, refactor, or test-only change
  • Documentation only

Additional information

  • This PR fixes or closes issue: fixes # None
  • This PR is related to: None
  • Link to a separate documentation pull request: None

Checklist

  • I fully understand the code in this pull request and can explain every line, including any AI-assisted changes.
  • The change is covered by tests, and poetry run pytest passes locally. A pull request cannot be merged unless CI is green.
  • poetry run prek run --all-files passes (lint, format, and type checks).
  • No commented-out or dead code is left in the pull request.

Parsing happened outside of the request error handling, so broken JSON or
a response that does not match the documented structure leaked raw
orjson, mashumaro, KeyError or TypeError exceptions. Callers that catch
RadioBrowserError, like Home Assistant, did not handle those.
@frenck frenck added the bugfix Inconsistencies or issues which will cause a problem for users or implementers. label Oct 1, 2026
@frenck
frenck merged commit c7c7c9b into main Oct 1, 2026
31 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bugfix Inconsistencies or issues which will cause a problem for users or implementers.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant