Skip to content

Commit

Permalink
Handle case when error message is a string. (#631)
Browse files Browse the repository at this point in the history
Fixes #630.
  • Loading branch information
keunhong authored Jul 18, 2024
1 parent c7cf66d commit 5cb4479
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
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

0 comments on commit 5cb4479

Please sign in to comment.