Repository navigation
Raise a RadioBrowserError for responses that cannot be parsed - #1566
Merged
Merged
Conversation
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.
4 of 11 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Breaking change
None. Calls that used to raise a raw parser exception on a broken response now raise a
RadioBrowserErrorinstead, 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:The README and
AGENTS.mdpromise that everything that goes wrong while talking to the API raises aRadioBrowserError, and Home Assistant relies on that: it catchesRadioBrowserError, 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 aRadioBrowserErrorwith 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
Additional information
Checklist
poetry run pytestpasses locally. A pull request cannot be merged unless CI is green.poetry run prek run --all-filespasses (lint, format, and type checks).