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
12 changes: 8 additions & 4 deletions src/storage/src/storage3/_async/bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,14 @@ async def _request(
)
response.raise_for_status()
except HTTPStatusError as exc:
resp = exc.response.json()
raise StorageApiError(
resp["message"], resp["error"], resp["statusCode"]
) from exc
try:
resp = exc.response.json()
raise StorageApiError(
resp["message"], resp["error"], resp["statusCode"]
) from exc
except (KeyError, ValueError) as err:
message = f"Unable to parse error message: {exc.response.text}"
raise StorageApiError(message, "InternalError", 400) from err

return response

Expand Down
12 changes: 8 additions & 4 deletions src/storage/src/storage3/_sync/bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,14 @@ def _request(
)
response.raise_for_status()
except HTTPStatusError as exc:
resp = exc.response.json()
raise StorageApiError(
resp["message"], resp["error"], resp["statusCode"]
) from exc
try:
resp = exc.response.json()
raise StorageApiError(
resp["message"], resp["error"], resp["statusCode"]
) from exc
except (KeyError, ValueError) as err:
message = f"Unable to parse error message: {exc.response.text}"
raise StorageApiError(message, "InternalError", 400) from err

return response

Expand Down
38 changes: 38 additions & 0 deletions src/storage/tests/_async/test_bucket.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from json import JSONDecodeError
from unittest.mock import AsyncMock, Mock

import pytest
Expand Down Expand Up @@ -198,6 +199,43 @@ async def test_request_error_handling(storage_api, mock_client) -> None:
assert exc_info.value.message == "Test error message"


async def test_request_error_handling_unrecognized_body(
storage_api, mock_client
) -> None:
error_response = Mock(spec=Response)
error_response.json.return_value = {"code": "TooLarge"}
error_response.text = '{"code": "TooLarge"}'

exc = HTTPStatusError("HTTP Error", request=Mock(), response=error_response)
mock_client.request.side_effect = exc

with pytest.raises(StorageApiError) as exc_info:
await storage_api._request("GET", ["test"])

assert (
exc_info.value.message == 'Unable to parse error message: {"code": "TooLarge"}'
)
assert exc_info.value.code == "InternalError"


async def test_request_error_handling_non_json_body(storage_api, mock_client) -> None:
error_response = Mock(spec=Response)
error_response.json.side_effect = JSONDecodeError("Expecting value", "", 0)
error_response.text = "<html>504 Gateway Time-out</html>"

exc = HTTPStatusError("HTTP Error", request=Mock(), response=error_response)
mock_client.request.side_effect = exc

with pytest.raises(StorageApiError) as exc_info:
await storage_api._request("GET", ["test"])

assert (
exc_info.value.message
== "Unable to parse error message: <html>504 Gateway Time-out</html>"
)
assert exc_info.value.code == "InternalError"


@pytest.mark.parametrize(
"method,path,json_data",
[
Expand Down
36 changes: 36 additions & 0 deletions src/storage/tests/_sync/test_bucket.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from json import JSONDecodeError
from unittest.mock import Mock

import pytest
Expand Down Expand Up @@ -198,6 +199,41 @@ def test_request_error_handling(storage_api, mock_client) -> None:
assert exc_info.value.message == "Test error message"


def test_request_error_handling_unrecognized_body(storage_api, mock_client) -> None:
error_response = Mock(spec=Response)
error_response.json.return_value = {"code": "TooLarge"}
error_response.text = '{"code": "TooLarge"}'

exc = HTTPStatusError("HTTP Error", request=Mock(), response=error_response)
mock_client.request.side_effect = exc

with pytest.raises(StorageApiError) as exc_info:
storage_api._request("GET", ["test"])

assert (
exc_info.value.message == 'Unable to parse error message: {"code": "TooLarge"}'
)
assert exc_info.value.code == "InternalError"


def test_request_error_handling_non_json_body(storage_api, mock_client) -> None:
error_response = Mock(spec=Response)
error_response.json.side_effect = JSONDecodeError("Expecting value", "", 0)
error_response.text = "<html>504 Gateway Time-out</html>"

exc = HTTPStatusError("HTTP Error", request=Mock(), response=error_response)
mock_client.request.side_effect = exc

with pytest.raises(StorageApiError) as exc_info:
storage_api._request("GET", ["test"])

assert (
exc_info.value.message
== "Unable to parse error message: <html>504 Gateway Time-out</html>"
)
assert exc_info.value.code == "InternalError"


@pytest.mark.parametrize(
"method,path,json_data",
[
Expand Down