How to catch Boto3 errors

Catching errors with the boto3 library is straightfoward.

Catching boto3 errors with ClientError

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
import boto3
import botocore

s3 = boto3.client("s3")

try:
    s3.get_object(Bucket="bucket", Key="key")
except botocore.exceptions.ClientError as e:
    if e.response["Error"]["Code"] == "AccessDenied":
        print(e.response["Error"]["Message"])
    else:
        raise

Other information in the e.response object may additionally be useful:

  • e.response["Error"]["Code"] for the error code
  • e.response["Error"]["Message"] for the error message
  • e.response["ResponseMetadata"]["RequestId"] for the request id
  • e.response["ResponseMetadata"]["HTTPStatusCode"] for the status code

e.response’s full dictionary:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
{
    'Error': {
        'Code': 'AccessDenied',
        'Message': 'Access Denied'
    }, 
    'ResponseMetadata': {
        'RequestId': 'requestId',
        'HostId': 'hostId',
        'HTTPStatusCode': 403,
        'HTTPHeaders': {
            'x-amz-request-id': 'requestId',
            'x-amz-id-2': 'id-2',
            'content-type': 'application/xml',
            'transfer-encoding': 'chunked',
            'date': 'Sat, 04 Mar 2023 06:11:15 GMT',
            'server': 'AmazonS3'
        },
        'RetryAttempts': 0
    }
}

Catching boto3 errors with service exceptions

For some clients, the AWS Python SDK has exposed service exceptions:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
import boto3
import botocore

s3 = boto3.client("s3")

try:
    s3.create_bucket(Bucket="bucket")
except s3.exceptions.BucketAlreadyExists:
    print("s3 bucket already exists")
except botocore.exceptions.ClientError as e:
    print(e)