Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/storage/src/storage3/_async/file_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ async def _request(
resp["message"], resp["error"], resp["statusCode"]
) from exc
except KeyError as err:
message = f"Unable to parse error message: {resp.text}"
message = f"Unable to parse error message: {exc.response.text}"
raise StorageApiError(message, "InternalError", 400) from err

# close the resource before returning the response
Expand Down
2 changes: 1 addition & 1 deletion src/storage/src/storage3/_sync/file_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def _request(
resp["message"], resp["error"], resp["statusCode"]
) from exc
except KeyError as err:
message = f"Unable to parse error message: {resp.text}"
message = f"Unable to parse error message: {exc.response.text}"
raise StorageApiError(message, "InternalError", 400) from err

# close the resource before returning the response
Expand Down
29 changes: 29 additions & 0 deletions src/storage/tests/_async/test_file_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from __future__ import annotations

import httpx
import pytest
from httpx import Headers
from storage3._async.file_api import AsyncBucketProxy
from storage3.exceptions import StorageApiError
from yarl import URL


async def test_request_raises_real_error_when_body_missing_message_field() -> None:
"""A non-2xx body without message/error/statusCode must surface as StorageApiError."""

def handler(request: httpx.Request) -> httpx.Response:
return httpx.Response(400, json={"code": "TooLarge"}, request=request)

async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
proxy = AsyncBucketProxy(
id="bucket",
_base_url=URL("http://localhost:54321/storage/v1"),
_headers=Headers(),
_client=client,
)
with pytest.raises(StorageApiError) as exc_info:
await proxy._request("GET", ["object", "open", "bucket", "file.txt"])

assert exc_info.value.code == "InternalError"
assert exc_info.value.status == 400
assert "TooLarge" in exc_info.value.message
29 changes: 29 additions & 0 deletions src/storage/tests/_sync/test_file_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from __future__ import annotations

import httpx
import pytest
from httpx import Headers
from storage3._sync.file_api import SyncBucketProxy
from storage3.exceptions import StorageApiError
from yarl import URL


def test_request_raises_real_error_when_body_missing_message_field() -> None:
"""A non-2xx body without message/error/statusCode must surface as StorageApiError."""

def handler(request: httpx.Request) -> httpx.Response:
return httpx.Response(400, json={"code": "TooLarge"}, request=request)

with httpx.Client(transport=httpx.MockTransport(handler)) as client:
proxy = SyncBucketProxy(
id="bucket",
_base_url=URL("http://localhost:54321/storage/v1"),
_headers=Headers(),
_client=client,
)
with pytest.raises(StorageApiError) as exc_info:
proxy._request("GET", ["object", "open", "bucket", "file.txt"])

assert exc_info.value.code == "InternalError"
assert exc_info.value.status == 400
assert "TooLarge" in exc_info.value.message