Skip to content

How to catch botocore.errorfactory errors when using awswrangler? #2749

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

Closed
nabelekt opened this issue Mar 26, 2024 · 2 comments
Closed

How to catch botocore.errorfactory errors when using awswrangler? #2749

nabelekt opened this issue Mar 26, 2024 · 2 comments
Labels
question Further information is requested

Comments

@nabelekt
Copy link

I am moving the discussion at #2723 here. It seems like those discussions might not be seen.

I have this simplified test code:

import awswrangler
import botocore

query = f"SELECT * FROM table_that_does_not_exist"
print(f"Executing statement: {query}\n...", flush=True)
response = awswrangler.dynamodb.execute_statement(query)

try:
    # This throws: botocore.errorfactory.ResourceNotFoundException: An error occurred (ResourceNotFoundException) when calling the ExecuteStatement operation: Requested resource not found
    print(list(response))
except botocore.errorfactory.ResourceNotFoundException:
    # This throws AttributeError: module 'botocore.errorfactory' has no attribute 'ResourceNotFoundException'
    print("Exception successfully caught.")

How can I properly catch the botocore.errorfactory.ValidationException error?

Here is the beset that I have been able to come up with:

import awswrangler
from botocore.exceptions import ClientError

query = f"SELECT * FROM table_that_does_not_exist"
print(f"Executing statement: {query}\n...", flush=True)
response = awswrangler.dynamodb.execute_statement(query)

try:
    # This throws: botocore.errorfactory.ResourceNotFoundException: An error occurred (ResourceNotFoundException) when calling the ExecuteStatement operation: Requested resource not found
    print(list(response))
except ClientError as e:
    if e.response['Error']['Code'] == 'ResourceNotFoundException':
        print("Exception successfully caught.")

Is this the best way to catch botocore exceptions when using awswrangler?

@nabelekt nabelekt added the question Further information is requested label Mar 26, 2024
@jaidisido
Copy link
Contributor

jaidisido commented Mar 27, 2024

This question relates to botocore not this library, but yes your second try/catch is one way to capture the exception

@jaidisido jaidisido closed this as not planned Won't fix, can't repro, duplicate, stale Mar 27, 2024
@nabelekt
Copy link
Author

nabelekt commented Apr 9, 2024

An alternative suggested by AWS support is to do something like this

import boto3

REGION="us-west-2"

session = boto3.Session()
ts_client = session.client('timestream-query')

try:
    records_df = ts_client.query(query)
except client.exceptions.ValidationException as e:
    records_df = pd.DataFrame()

but to me, it seems silly to create a boto3 session just to catch exceptions. I assume awswrangler is using a boto3 session. Is it accessible for us to use?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants