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

Request Id in Errors #884

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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
17 changes: 16 additions & 1 deletion s3fs/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,15 +140,30 @@ def translate_boto_error(error, message=None, set_cause=True, *args, **kwargs):
if error_response is None:
# non-http error, or response is None:
return error

# AWS s3 as well as moto (CI Backend) respond with ResponseMetadata that contains RequestId as well as
# HTTPStatusCode. As of 6/17/2024, minio doesn't seem to add RequestId to the response.
request_id = ''
if 'ResponseMetadata' in error_response and 'RequestId' in error_response['ResponseMetadata']:
request_id = error_response['ResponseMetadata']['RequestId']
http_code = ''
if 'ResponseMetadata' in error_response and 'HTTPStatusCode' in error_response['ResponseMetadata']:
http_code = error_response['ResponseMetadata']['HTTPStatusCode']

code = error_response["Error"].get("Code")
constructor = ERROR_CODE_TO_EXCEPTION.get(code)
if constructor:
if not message:
message = error_response["Error"].get("Message", str(error))
message = f'{message}, Request ID: {request_id}' if request_id else message
message = f'{message}, HTTP Status code: {http_code}' if http_code else message
custom_exc = constructor(message, *args, **kwargs)
else:
# No match found, wrap this in an IOError with the appropriate message.
custom_exc = IOError(errno.EIO, message or str(error), *args)
message = message or str(error)
message = f'{message}, Request ID: {request_id}' if request_id else message
message = f'{message}, HTTP Status code: {http_code}' if http_code else message
custom_exc = IOError(errno.EIO, message, *args)

if set_cause:
custom_exc.__cause__ = error
Expand Down
24 changes: 24 additions & 0 deletions s3fs/tests/test_errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import pytest
from s3fs.tests.test_s3fs import s3_base, s3, test_bucket_name
from s3fs import S3Map, S3FileSystem

root = test_bucket_name + "/mapping"


def test_simple(s3):
d = s3.get_mapper(root)
assert not d

assert list(d) == list(d.keys()) == []
assert list(d.values()) == []
assert list(d.items()) == []
s3.get_mapper(root)

try:
# Make an operation that raises IOError or OSError
f = d["nonexistent"]
print(f)
except OSError as e:
# verify error details
...

2 changes: 1 addition & 1 deletion s3fs/tests/test_s3fs.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def get_boto3_client():

# NB: we use the sync botocore client for setup
session = Session()
return session.create_client("s3", endpoint_url=endpoint_uri)
return session.create_client("s3", endpoint_url=endpoint_uri, region_name="us-east-1")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this necessary?



@pytest.fixture()
Expand Down
Loading