Repository navigation
fix: handle non-standard and non-JSON error responses without AttributeError - #1577
Merged
Merged
Conversation
…ut AttributeError
Contributor
Author
|
@o-santi can you please review the PR? |
This was referenced Sep 2, 2026
A 502 with content-type: application/json and a body of [], null, "..." or a number parses fine but is not subscriptable by string key, so resp["message"] raised a bare TypeError instead of StorageApiError.
grdsdev
approved these changes
Sep 3, 2026
_request now converts JSONDecodeError into StorageApiError, so exists() no longer saw the JSONDecodeError it relied on. HEAD responses carry no body, so a missing object arrives as an unparsable 400/404; treat those as False and re-raise anything else.
This was referenced Sep 3, 2026
This was referenced Oct 20, 2026
spydon
added a commit
that referenced
this pull request
Oct 28, 2026
Follow-up to #1577. Neither fix from that PR is needed on `v3` — pydantic's `ValidationError` already covers every malformed body shape, and `exists()` already branches on status rather than on an exception type. But verifying that surfaced a separate bug in the same code. ### The bug `parse_api_error` validated the response body against `StorageApiError` itself, whose fields are `message` / `code` / `status`. The API sends `statusCode` / `error` / `message`. No aliases are configured, so validation always failed and **every** API error fell into the fallback: | body | before | after | |---|---|---| | `{"statusCode":"404","error":"not_found","message":"Object not found"}` | `code='InternalError' status=400` | `code='not_found' status='404'` | | `<html>502 Bad Gateway</html>` | `code='InternalError' status=400` | `code='InternalError' status=502` | A 404 was indistinguishable from a 500, and the real message was buried in `Unable to parse error message: {raw json}`. This affects all 9 `parse_api_error` call sites across `file_api.py` and `vectors.py`. Two things suggest an oversight rather than a deliberate shape change: - `VectorBucketErrorMessage` sat in the same file describing the correct wire shape, imported nowhere. - The two existing error tests only assert `pytest.raises(StorageApiError)`, never `.code` or `.status` — so the suite could not see it. Their mock body is already in the correct wire format. ### Changes - Parse into `StorageApiErrorMessage` (the renamed, previously-dead model), then build `StorageApiError` from it. `VectorBucketErrorMessage` is kept as an alias — the vector endpoints return the same body. - Fallback reports `response.status` instead of a hardcoded `400`. - Fallback decodes with `errors="replace"`, so a non-utf-8 body cannot raise from the error path itself. - New `tests/test_exceptions.py`: 9 unit tests, no infra needed. All 9 fail on the current `v3` code and pass with this change. - Tightened the two existing `test_client_info_with_error` tests to assert `.message`, `.code` and `.status`. ### Verification `ruff check`, `ruff format` and `mypy` clean; the 9 new unit tests pass. Docker is not available in my environment, so the integration suite was not run locally — CI covers it. --------- Co-authored-by: Lukas Klingsbo <[email protected]>
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.
Summary
Fixes an
AttributeErrorinstorage3where non-2xx responses that lackmessage,error, orstatusCodekeys in their JSON body (or return non-JSON payloads like HTML 502/504 gateway errors) crashed with'dict' object has no attribute 'text'or uncaughtJSONDecodeError.Changes
_async/file_api.py,_async/bucket.py, and_synccounterparts:(KeyError, ValueError)when decoding error response JSON.exc.response.textinstead ofresp.text.exc.response.status_codetoStorageApiError.test_client.pyandtest_bucket.py(both async and sync).Verification
src/storagepass.mypycheck passes with 0 issues.ruff checkandruff formatpass cleanly.