Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

Closed
omadawn opened this issue Mar 6, 2024 · 1 comment
Closed

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

omadawn opened this issue Mar 6, 2024 · 1 comment
Assignees
Labels
bug This issue is a confirmed bug.

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
@tim-finnigan tim-finnigan self-assigned this May 17, 2024
@tim-finnigan tim-finnigan added the investigating This issue is being investigated and/or work is in progress to resolve the issue. label May 17, 2024
@tim-finnigan
Copy link
Contributor

Thanks for reaching out and your patience here. The recommended approach is to catch the ClientError exception raised by Boto3 and check the error code within the exception. The Boto3 documentation on error handling service exceptions provides more context. Here's an example:

import boto3
from botocore.exceptions import ClientError

s3 = boto3.client('s3')

try:
    response = s3.get_bucket_location(Bucket='nonexistent-bucket')
except ClientError as e:
    if e.response['Error']['Code'] == 'NoSuchBucket':
        print('The specified bucket does not exist.')
    else:
        raise e

I also see that the version of Boto3 you're using is quite old, please consider updating to a newer version for access to the latest updates. (The latest version is 1.34.107 per the CHANGELOG). If you have any follow up questions please let us know. I'm going to convert this from an issue to a discussion as it is not a bug.

@tim-finnigan tim-finnigan removed investigating This issue is being investigated and/or work is in progress to resolve the issue. needs-triage This issue or PR still needs to be triaged. labels May 17, 2024
@boto boto locked and limited conversation to collaborators May 17, 2024
@tim-finnigan tim-finnigan converted this issue into discussion #4134 May 17, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
bug This issue is a confirmed bug.
Projects
None yet
Development

No branches or pull requests

2 participants