Carl
Carl

Reputation: 2934

Where to look-up what exceptions a BOTO3 function can throw?

I’m reading AWS Python docs such as SNS Client Publish() but can’t find the details of what exceptions a function can throw.

E.g., publish() can throw EndpointDisabledException but I can’t find this documented.

Where can I look up the list of exceptions a BOTO3 function can throw (for Python)

Upvotes: 11

Views: 12095

Answers (5)

Vijay Chavda
Vijay Chavda

Reputation: 862

Following code will generate an exhaustive list of exceptions from all supported services that it's boto3 client can throw.

#!/usr/bin/env python3
import boto3

with open("README.md", "w") as f:
    f.write("# boto3 client exceptions\n\n")
    f.write(
        "This is a generated list of all exceptions for each client within the boto3 library\n"
    )
    f.write("""
```python
import boto3
s3 = boto3.client("s3")
try:
  s3.create_bucket("example")
except s3.exceptions.BucketAlreadyOwnedByYou:
  raise
```\n\n""")
    services = boto3.session.Session().get_available_services()

    for service in services:
        f.write(f"- [{service}](#{service})\n")

    for service in services:
        f.write(f"### {service}\n")
        for _, value in boto3.client(
            service, region_name="us-east-1"
        ).exceptions.__dict__.items():
            for exception in value:
                f.write(f"- {exception}\n")
        f.write("\n")

Example output for S3 boto3 client:

  • BucketAlreadyExists
  • BucketAlreadyOwnedByYou
  • InvalidObjectState
  • NoSuchBucket
  • NoSuchKey
  • NoSuchUpload
  • ObjectAlreadyInActiveTierError
  • ObjectNotInActiveTierError

Reference: https://github.com/jbpratt/boto3-client-exceptions/blob/master/generate

Upvotes: 0

mikowhy
mikowhy

Reputation: 16

you can find the list of exceptions for publish(**kwargs) -> here <- (bottom of publish(**kwargs) part)

Every exception is linked to its documentation

SNS.Client.exceptions.InvalidParameterException
SNS.Client.exceptions.InvalidParameterValueException
SNS.Client.exceptions.InternalErrorException
SNS.Client.exceptions.NotFoundException
SNS.Client.exceptions.EndpointDisabledException
SNS.Client.exceptions.PlatformApplicationDisabledException
SNS.Client.exceptions.AuthorizationErrorException
SNS.Client.exceptions.KMSDisabledException
SNS.Client.exceptions.KMSInvalidStateException
SNS.Client.exceptions.KMSNotFoundException
SNS.Client.exceptions.KMSOptInRequired
SNS.Client.exceptions.KMSThrottlingException
SNS.Client.exceptions.KMSAccessDeniedException
SNS.Client.exceptions.InvalidSecurityException

Upvotes: 0

satej sarker
satej sarker

Reputation: 328

Use the client and then find the exception

example : if we are dealing with the cognito then

client = boto3.client(
        'cognito-idp',....)
try:
   some code .......
except client.exceptions.UsernameExistsException as ex:
    print(ex)

Upvotes: 0

Carl
Carl

Reputation: 2934

This is how to handle such exceptions:

import boto3
from botocore.exceptions import ClientError
import logging

try:
    response = platform_endpoint.publish(
        Message=json.dumps(message, ensure_ascii=False),
        MessageStructure='json')
    logging.info("r = %s" % response)
except ClientError as e:
    if e.response['Error']['Code'] == 'EndpointDisabled':
        logging.info('EndpointDisabledException thrown')

Upvotes: 8

helloV
helloV

Reputation: 52463

Almost all exceptions are subclassed from BotoCoreError. I am not able to find a method to list all exceptions. Look at Botocore Exceptions file to get a list of possible exceptions. I can't find EndpointDisabledException. Are you using the latest version?

See: Botocore Exceptions

Upvotes: 7

Related Questions