Insight Horizon
education insights /

Error handling — Boto3 Docs 1.16.26 documentation

Catching botocore exceptions

Botocore exceptions are statically defined in the botocore package. Any Boto3 clients you create will use these same statically defined exception classes. 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.

Additional client-side issues with SSL negotiation, client misconfiguration, or AWS service validation errors will also throw botocore exceptions. Here’s a generic example of how you might catch botocore exceptions.

import botocoreimport boto3client = boto3.client('aws_service_name')try: client.some_api_call(SomeParam='some_param')except botocore.exceptions.ClientError as error: # Put your error handling logic here raise errorexcept botocore.exceptions.ParamValidationError as error: raise ValueError('The parameters you provided are incorrect: {}'.format(error))

Parsing error responses and catching exceptions from AWS services

Unlike botocore exceptions, AWS service exceptions aren't statically defined in Boto3. This is due to errors and exceptions from AWS services varying widely and being subject to change. To properly catch an exception from an AWS service, you must parse the error response from the service. The error response provided to your client from the AWS service follows a common structure and is minimally processed and not obfuscated by Boto3.

Using Boto3, the error response from an AWS service will look similar to a success response, except that an Error nested dictionary will appear with the ResponseMetadata nested dictionary. Here is an example of what an error response might look like:

{ 'Error': { 'Code': 'SomeServiceException', 'Message': 'Details/context around the exception or error' }, 'ResponseMetadata': { 'RequestId': '1234567890ABCDEF', 'HostId': 'host ID data will appear here as a hash', 'HTTPStatusCode': 400, 'HTTPHeaders': {'header metadata key/values will appear here'}, 'RetryAttempts': 0 }}

Boto3 classifies all AWS service errors and exceptions as ClientError exceptions. When attempting to catch AWS service exceptions, one way is to catch ClientError and then parse the error response for the AWS service-specific exception.

Using Amazon Kinesis as an example service, you can use Boto3 to catch the exception LimitExceededException and insert your own logging message when your code experiences request throttling from the AWS service.

import botocoreimport boto3import logging# Set up our loggerlogging.basicConfig(level=logging.INFO)logger = logging.getLogger()client = boto3.client('kinesis')try: logger.info('Calling DescribeStream API on myDataStream') client.describe_stream(StreamName='myDataStream')except botocore.exceptions.ClientError as error: if error.response['Error']['Code'] == 'LimitExceededException': logger.warn('API call limit exceeded; backing off and retrying...') else: raise error

Note

The Boto3 standard retry mode will catch throttling errors and exceptions, and will back off and retry them for you.

Additionally, you can also access some of the dynamic service-side exceptions from the client’s exception property. Using the previous example, you would need to modify only the except clause.

except client.exceptions.LimitExceedException as error: logger.warn('API call limit exceeded; backing off and retrying...')

Note

Catching exceptions through ClientError and parsing for error codes is still the best way to catch all service-side exceptions and errors.