'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)
Solution 1:[1]
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')
Solution 2:[2]
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
Solution 3:[3]
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)
Solution 4:[4]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Carl |
| Solution 2 | helloV |
| Solution 3 | satej sarker |
| Solution 4 | Vijay Chavda |
