Skip to content

fix: handle non-standard and non-JSON error responses without AttributeError - #1577

Merged
grdsdev merged 3 commits into
supabase:mainfrom
Vishv07:fix-storage3-error-parsing
Sep 3, 2026
Merged

grdsdev merged 3 commits into
supabase:mainfrom
Vishv07:fix-storage3-error-parsing

Conversation

@Vishv07

@Vishv07 Vishv07 commented Aug 16, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes an AttributeError in storage3 where non-2xx responses that lack message, error, or statusCode keys in their JSON body (or return non-JSON payloads like HTML 502/504 gateway errors) crashed with 'dict' object has no attribute 'text' or uncaught JSONDecodeError.

Changes

  • In _async/file_api.py, _async/bucket.py, and _sync counterparts:
    • Catch (KeyError, ValueError) when decoding error response JSON.
    • Access exc.response.text instead of resp.text.
    • Pass exc.response.status_code to StorageApiError.
  • Added unit regression tests for missing error keys and non-JSON error responses across test_client.py and test_bucket.py (both async and sync).

Verification

  • All 51 unit tests in src/storage pass.
  • mypy check passes with 0 issues.
  • ruff check and ruff format pass cleanly.

@Vishv07
Vishv07 requested review from a team and o-santi as code owners August 16, 2026 05:06
@Vishv07

Vishv07 commented Aug 22, 2026

Copy link
Copy Markdown
Contributor Author

@o-santi can you please review the PR?

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 grdsdev changed the title Fix: handle non-standard and non-JSON error responses without AttributeError fix: handle non-standard and non-JSON error responses without AttributeError 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.
@grdsdev
grdsdev merged commit 5b8080a into supabase:main Sep 3, 2026
43 checks passed
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants