fix: prevent TogetherException repr crash on non-JSON-serializable headers#431
Open
giulio-leone wants to merge 1 commit intotogethercomputer:mainfrom
Open
Conversation
…aders When headers contain non-JSON-serializable objects (e.g. aiohttp's CIMultiDictProxy), `json.dumps()` in `__repr__` raises TypeError, making it impossible to print or log the exception. Add `default=str` to json.dumps so non-serializable objects fall back to their string representation instead of crashing. Fixes togethercomputer#108 Signed-off-by: Giulio Leone <6887247+giulio-leone@users.noreply.github.com>
6cd9a96 to
b9c7094
Compare
Author
|
Friendly ping — CI is green, tests pass, ready for review whenever convenient. Happy to address any feedback. Thanks! 🙏 |
Author
|
Friendly ping — this branch has been refreshed on the latest upstream base and all current review feedback has been addressed. It should be ready for review whenever you have a chance. Happy to make any further changes quickly. |
Author
|
Friendly ping — rebased on latest and ready for review. Happy to address any feedback! |
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.
Problem
TogetherException.__repr__usesjson.dumps()to serialize the exception's attributes (includingheaders) into a JSON string. Whenheaderscontains a non-JSON-serializable object like aiohttp'sCIMultiDictProxy, this raises aTypeError:This makes it impossible to
print(),repr(), or log the exception — the debugging tool itself crashes.Root Cause
json.dumps()in__repr__has no fallback for non-serializable types:Fix
Add
default=strtojson.dumps()so non-serializable objects fall back to their string representation:This is the standard Python pattern for graceful JSON serialization of arbitrary objects.
Tests
Added 7 unit tests covering:
CIMultiDictProxy-like headers (the reported bug)Noneheaders (default case)Verified Locally
All 7 new tests pass. All 181 existing unit tests continue to pass.
Fixes #108
Note
Low Risk
Small, isolated change to exception stringification plus tests; behavior only differs when
headers(or nested values) aren’t JSON-serializable.Overview
Prevents
TogetherException.__repr__from throwing whenheaderscontains non-JSON-serializable objects (e.g., aiohttpCIMultiDictProxy) by adding ajson.dumps(..., default=str)fallback.Adds a focused unit test suite (
tests/unit/test_error_repr.py) covering dict/None/string headers, nested non-serializable values, JSON parseability of the produced repr, and ensuring subclasses (e.g.AuthenticationError,ResponseError,APIError) inherit the behavior.Written by Cursor Bugbot for commit b9c7094. This will update automatically on new commits. Configure here.