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

Handling all possible errors from AWS / Boto #4103

Open
Hvass-Labs opened this issue Apr 27, 2024 · 2 comments
Open

Handling all possible errors from AWS / Boto #4103

Hvass-Labs opened this issue Apr 27, 2024 · 2 comments
Assignees
Labels
documentation This is a problem with documentation.

Comments

@Hvass-Labs
Copy link

Describe the issue

Hello,

I am using AWS SES to send e-mails. I am new to AWS and it is unclear from the docs how to handle all possible errors arising from AWS / Boto, so they can be logged and investigated later. The user doesn't need to know exactly what happened and will just see a generic error-message.

My code is essentially:

try:
    client = boto3.client('sesv2', region_name=AWS_REGION_NAME,
                          aws_access_key_id=AWS_ACCESS_KEY_ID,
                          aws_secret_access_key=AWS_SECRET_ACCESS_KEY)
                          
    try:
        response = client.send_email(...)
    finally:
        client.close()

    # Do I also have to check response['ResponseMetadata']['HTTPStatusCode'] != 200 ?

except botocore.exceptions.ClientError as e:
    logging.error(str(e))

except botocore.exceptions.BotoCoreError as e:
    logging.error(str(e))

Are these two exception classes sufficient to handle all possible errors arising in AWS / Boto?

Or do I also need to inspect the response object for other possible errors if the HTTP status-code isn't 200 for success?

Thanks!

Links

https://boto3.amazonaws.com/v1/documentation/api/latest/guide/error-handling.html

@Hvass-Labs Hvass-Labs added documentation This is a problem with documentation. needs-triage This issue or PR still needs to be triaged. labels Apr 27, 2024
@tim-finnigan tim-finnigan self-assigned this Apr 30, 2024
@tim-finnigan tim-finnigan added investigating This issue is being investigated and/or work is in progress to resolve the issue. and removed needs-triage This issue or PR still needs to be triaged. labels Apr 30, 2024
@tim-finnigan
Copy link
Contributor

Hi @Hvass-Labs thanks for reaching out. As mentioned in the error handling documentation that you referenced, it notes:

The most common botocore exception you’ll encounter is ClientError. This is a general exception when an error response is provided by an AWS service to your Boto3 client’s request.

So while it is possible to account for each specific exception, ClientError is the main one to account for. You can see all of the static exceptions in this toggle list on that documentation page:

image

And the full list of possible exceptions can be found in the exceptions.py file. For example BotoCoreError, which serves as a catchall for unspecified Botocore errors, but wouldn't account for things like networking-related errors.

I hope that helps - if you have any more questions please let us know. Or if you are encountering issues other than ClientError, we could look into those further. If you add boto3.set_stream_logger('') to your script, then that will print a full log stack trace to help give more insight into any issues.

@tim-finnigan tim-finnigan added response-requested Waiting on additional information or feedback. and removed investigating This issue is being investigated and/or work is in progress to resolve the issue. labels Apr 30, 2024
@Hvass-Labs
Copy link
Author

Thanks for the detailed reply! I did actually read the doc-page and noticed the section you highlight :-)

But it is still a bit unclear to me, so allow me to ask more specifically:

  1. Is it necessary to catch both BotoCoreError and ClientError to make sure I catch all custom exceptions that can be raised by Boto / AWS?

  2. The client.send_mail() function returns a response object. Is it necessary to also check it for HTTP error-codes e.g. response['ResponseMetadata']['HTTPStatusCode'] != 200, or will all HTTP error-codes cause the Boto library to raise an exception?

Thanks for the tip on logging the stack-trace. However, as this is running on a web-server, I don't log the stack-trace because I am worried it might contain the AWS account credentials, which I don't want in the log for security reasons. But perhaps I'm wrong?

@github-actions github-actions bot removed the response-requested Waiting on additional information or feedback. label May 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation This is a problem with documentation.
Projects
None yet
Development

No branches or pull requests

2 participants