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

Add ClientError getters for common attributes #3251

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

thiezn
Copy link

@thiezn thiezn commented Sep 11, 2024

Most errors returned from AWS APIs are captured in the ClientError class. When writing Python code that interacts with the AWS APIs, you frequently want to parse the specific error and/or error message.

The boto3 documentation describes how this can be accomplished by parsing the error code:

from botocore.exceptions import ClientError
import boto3

client = boto3.client('kinesis')

try:
    client.describe_stream(StreamName='myDataStream')
except ClientError as error:
    if error.response['Error']['Code'] == 'LimitExceededException':
        logger.warn('API call limit exceeded; backing off and retrying...')
    else:
        raise error

Looking at the botocore unit tests for exceptions there is a test case that checks if ClientError exceptions still work even if the Code or Message is not available.

That seems to indicate that this is an actual possibility which means the above suggested code could unexpectedly fail. To properly evaluate an error, the code should be something like this:

    if error.response.get('Error', {}).get('Code') == 'LimitExceededException':

This is even more verbose than the boto3 documentation indicates, and might impact existing customers.

There are additional fields available that provide even more details of the error that can be useful. Extract from the boto3 documentation gives the following example.

    if err.response['Error']['Code'] == 'InternalError': # Generic error
        # We grab the message, request ID, and HTTP code to give to customer support
        print('Error Message: {}'.format(err.response['Error']['Message']))
        print('Request ID: {}'.format(err.response['ResponseMetadata']['RequestId']))
        print('Http code: {}'.format(err.response['ResponseMetadata']['HTTPStatusCode']))

It's great the information is available, but it's not easy to find the appropriate dictionary key names without looking at the documentation.

This pull requests adds getters (property) to the ClientError class to extract these values. The added benefit is that IDE's will be able to autocomplete the getters and inline documentation. It will also lead to less verbose code for users of the library.

Here's an example of how it improves readability for the users of the library:

from botocore.exceptions import ClientError
import boto3

client = boto3.client('kinesis')

try:
    client.describe_stream(StreamName='myDataStream')
except ClientError as error:
    if error.code == 'LimitExceededException':
        print('API call limit exceeded; backing off and retrying...')
        print(error.message)
        print(error.request_id)
        print(error.http_status_code)
        print(error.http_headers)
    else:
        raise

…code, and request_id

This change adds the following properties to the ClientError class. These properties will make it easier to
work with the error object and extract information from it.

- code: The error code returned by the service.
- message: The error message returned by the service.
- http_headers: The HTTP headers returned by the service.
- http_status_code: The HTTP status code returned by the service.
- request_id: The request ID returned by the service.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants