Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle case when error message is a string. #631

Merged
merged 1 commit into from
Jul 18, 2024
Merged
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
8 changes: 6 additions & 2 deletions gcsfs/retry.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,11 @@ def validate_response(status, content, path, args=None):
content = content.decode()
try:
error = json.loads(content)["error"]
msg = error["message"]
# Sometimes the error message is a string.
if isinstance(error, str):
msg = error
else:
msg = error["message"]
except json.decoder.JSONDecodeError:
msg = content

Expand All @@ -109,7 +113,7 @@ def validate_response(status, content, path, args=None):
raise requests.exceptions.ProxyError()
elif "invalid" in str(msg):
raise ValueError(f"Bad Request: {path}\n{msg}")
elif error:
elif error and not isinstance(error, str):
raise HttpError(error)
elif status:
raise HttpError({"code": status, "message": msg}) # text-like
Expand Down
9 changes: 9 additions & 0 deletions gcsfs/tests/test_retry.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,15 @@ def test_validate_response():
validate_response(502, b"", "/path")


def test_validate_response_error_is_string():
# HttpError with JSON body
j = '{"error": "Too Many Requests"}'
with pytest.raises(HttpError) as e:
validate_response(429, j, "/path")
assert e.value.code == 429
assert e.value.message == "Too Many Requests, 429"


@pytest.mark.parametrize(
["file_path", "validate_get_error", "validate_list_error", "expected_error"],
[
Expand Down
Loading