Handling all possible errors from AWS / Boto #4153
Replies: 6 comments 1 reply
-
Hi @Hvass-Labs thanks for reaching out. As mentioned in the error handling documentation that you referenced, it notes:
So while it is possible to account for each specific exception, 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 |
Beta Was this translation helpful? Give feedback.
-
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:
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? |
Beta Was this translation helpful? Give feedback.
-
Thanks for following up and your patience. I think the snippet below helps show what you can do here. You can use import boto3
import botocore
ses_client = boto3.client("sesv2")
# Define the email parameters
# ...
try:
response = ses_client.send_email(
FromEmailAddress=sender_email,
Destination=destination,
Content=message
)
print("Email sent! Message ID:", response['MessageId'])
except botocore.exceptions.ClientError as error:
# Handle specific AWS service errors
if error.response['Error']['Code'] == 'MessageRejected':
print("Error: Message rejected by Amazon SES:", error.response['Error']['Message'])
else:
print("An error occurred while sending the email:", error)
except botocore.exceptions.ParamValidationError as error:
raise ValueError('The parameters you provided are incorrect: {}'.format(error))
except Exception as error:
# Handle any other exceptions
print("An unexpected error occurred:", error) Also in case you didn't see it I will highlight this section on parsing error responses, with I think this part being the most relevant here:
So as long as you're catching For more information on logging please see the boto3.set_stream_logger documentation. As mentioned there:
But also note the Warning:
|
Beta Was this translation helpful? Give feedback.
-
Greetings! It looks like this issue hasn’t been active in longer than five days. We encourage you to check if this is still an issue in the latest release. In the absence of more information, we will be closing this issue soon. If you find that this is still a problem, please feel free to provide a comment or upvote with a reaction on the initial post to prevent automatic closure. If the issue is already closed, please feel free to open a new one. |
Beta Was this translation helpful? Give feedback.
-
Thanks for the example. Maybe I am being a stickler, but I'm still not sure it answers my two questions above :-) Would it be possible to give yes / no answers to those two questions? My project is running on a web-server where I give broad error-messages to the user for different kinds of error-types. I am catching both |
Beta Was this translation helpful? Give feedback.
-
Sure I'll try to clarify:
No - to catch every exception, you would need to catch every documented exception. As mentioned in the docs,
No - it isn't necessary to also check for HTTP error codes. But depending on the service/API and your use case, you may want to check. For example with SNS the PublishBatch API documentation says:
In the case of S3 copy requests, if an error is found with a 200 status code then it is changed here to 500. Converting this to a Q&A discussion. If you want to create a new issue with a specific request please feel free to. |
Beta Was this translation helpful? Give feedback.
-
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:
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
Beta Was this translation helpful? Give feedback.
All reactions