AWS Error Utils

2.7.0 · active · verified Wed Apr 15

AWS Error Utils is a Python library that provides error-handling functions to simplify dealing with `botocore.exceptions.ClientError` in boto3 applications. It aims to make AWS service exception handling more Pythonic by allowing direct matching against error codes and operation names, reducing the need to parse nested dictionary structures. The current version is 2.7.0, and it maintains a steady release cadence with minor updates addressing Python version support and new features.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `catch_aws_error` to handle specific AWS `ClientError` exceptions. It attempts to access a non-existent S3 bucket, which typically raises a `NoSuchBucket` error. It also shows the alternative `errors.NoSuchBucket` syntax, which is an alias for `catch_aws_error('NoSuchBucket')`.

import boto3
from aws_error_utils import catch_aws_error, errors
import os
from botocore.exceptions import ClientError

# Configure boto3 client with dummy credentials for local testing, or load from environment
s3_client = boto3.client(
    's3',
    aws_access_key_id=os.environ.get('AWS_ACCESS_KEY_ID', 'test'),
    aws_secret_access_key=os.environ.get('AWS_SECRET_ACCESS_KEY', 'test'),
    aws_session_token=os.environ.get('AWS_SESSION_TOKEN'), # Optional
    region_name=os.environ.get('AWS_DEFAULT_REGION', 'us-east-1')
)

non_existent_bucket = "non-existent-aws-error-utils-test-bucket-12345"

print(f"Attempting to list objects in '{non_existent_bucket}'...")
try:
    s3_client.list_objects_v2(Bucket=non_existent_bucket)
except catch_aws_error("NoSuchBucket") as e:
    print(f"Caught expected AWS error using catch_aws_error: Code={e.code}, Message='{e.message}'")
except ClientError as e:
    print(f"Caught an unexpected ClientError: {e}")
except Exception as e:
    print(f"Caught a generic exception: {e}")

print("\nDemonstrating 'errors' class for common error codes (if applicable) (alias to catch_aws_error)...")
try:
    s3_client.list_objects_v2(Bucket=non_existent_bucket)
except errors.NoSuchBucket as e:
    print(f"Caught expected AWS error using errors.NoSuchBucket: Code={e.code}, Message='{e.message}'")
except ClientError as e:
    print(f"Caught an unexpected ClientError via ClientError: {e}")

view raw JSON →