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

Exceptions can't be caught with try/except #4043

Open
omadawn opened this issue Mar 6, 2024 · 0 comments
Open

Exceptions can't be caught with try/except #4043

omadawn opened this issue Mar 6, 2024 · 0 comments
Labels
bug This issue is a confirmed bug. needs-triage This issue or PR still needs to be triaged.

Comments

@omadawn
Copy link

omadawn commented Mar 6, 2024

Describe the bug

When boto3 throws an exception and we attempt to catch it, it fails because the exception does not actually exist.

Was there a reason this issue #1195 closed? The problem persists.

Expected Behavior

The reported errror indicates an exception which can actually be caught

Current Behavior

When running the first two code snippets the interpreter throws a trace which includes the error botocore.errorfactory.NoSuchBucket

  File "/path/to/some/venv/lib/python3.11/site-packages/botocore/client.py", line 915, in _make_api_call
    raise error_class(parsed_response, operation_name)
botocore.errorfactory.NoSuchBucket: An error occurred (NoSuchBucket) when calling the PutObject operation: The specified bucket does not exist

But when updating the code to try and catch that the interpreter doesn't know what botocore.errorfactory.NoSuchBucket is

  File "/path/to/my/script.py", line 400, in update_s3_files
    except botocore.errorfactory.NoSuchBucket:
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: module 'botocore.errorfactory' has no attribute 'NoSuchBucket'

Reproduction Steps

Run the following code:

import boto3

boto3_session = boto3.Session(profile_name='dev')
b3_client = boto3_session.client(service_name="s3")

b = 'somebucketnamewhichdoesntexist'

response = b3_client.get_bucket_location(
    Bucket=b
)

Because that bucket doesn't exist, boto3 throws a botocore.errorfactory.NoSuchBucket error.

Add botocore and copy and paste botocore.errorfactory.NoSuchBucket into an except block and run it.

import botocore
import boto3

boto3_session = boto3.Session(profile_name='dev')
b3_client = boto3_session.client(service_name="s3")

b = 'somebucketnamewhichdoesntexist'

try:
    response = b3_client.get_bucket_location(
        Bucket=b
    )
except botocore.errorfactory.NoSuchBucket as e:
    print('bucket %s doesnt exist')

Possible Solution

The current workaround to the try/except block is to catch client.exceptions.NoSuchBucket

Which isn't a very usable solution since there is nothing in the error message to tell you what to do.

Proposed solutions:

  1. Return the error in a way that indicates that we should be using the client.
    b3_client.errorfactory.NoSuchBucket: An error occurred (NoSuchBucket) when calling the PutObject operation: The specified bucket does not exist

  2. Update boto3 so that there is are accessible error objects such as boto3.errors.NoSuchBucket and return that in the error message.

  3. Update botocore so that the error exists.

Additionally code using boto3.resource() throws the same error.

import boto3

session = boto3.session.Session(profile_name='dev')
s3 = session.resource('s3')
object = s3.Object('my_bucket_name', 'my/key/including/filename.txt')
object.put(Body=b'some binary data')

Additional Information/Context

All of the code examples throw the same errors if boto3.session is not used. They include that to simplify testing in environments which use SSO with aws.

SDK version used

boto3==1.24.13 botocore==1.27.13

Environment details (OS name and version, etc.)

MacOS 14.2.1

@omadawn omadawn added bug This issue is a confirmed bug. needs-triage This issue or PR still needs to be triaged. labels Mar 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue is a confirmed bug. needs-triage This issue or PR still needs to be triaged.
Projects
None yet
Development

No branches or pull requests

1 participant