Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to catch exceptions from botocore

I am getting the below error while using boto3 with Amazon SNS. I want to catch InvalidParameterException only, how can I do the same?

Traceback (most recent call last):
  File "D:\Logger\Notification.py", line 279, in <module>
    Push.subscribe(token1, 'android')
  File "D:\Logger\Notification.py", line 119, in subscribe
    'Enabled': b'True'
  File "C:\Python27\lib\site-packages\botocore\client.py", line 310, in _api_call
    return self._make_api_call(operation_name, kwargs)
  File "C:\Python27\lib\site-packages\botocore\client.py", line 599, in _make_api_call
    raise error_class(parsed_response, operation_name)
botocore.errorfactory.InvalidParameterException: An error occurred (InvalidParameter) when calling the CreatePlatformEndpoint operation: Invalid parameter: Token Reason: Endpoint arn:aws:sns:us-west-2:252285631092:endpoint/GCM/Test/06c4448e-545b-312a-978f-98af5d5829e4 already exists with the same Token, but different attributes.

If I try to catch InvalidParameterException, it shows

NameError: global name 'InvalidParameterException' is not defined

I have imported botocore. Now if I try to catch botorcore.errorfactory.InvalidParameterException it shows.

AttributeError: 'module' object has no attribute 'InvalidParameterException'
like image 797
garg10may Avatar asked Sep 06 '25 03:09

garg10may


1 Answers

The botocore library generates several exceptions from a base class. Catch the baseclass:

from botocore.exceptions import ClientError

try:    
    ...
except ClientError as e:
    ...

Inspect the ClientError().response['Error']['Code'] to vary how you handle the exception, and just raise again if you want to ignore a specific error type. See the Error Handling documentation.

like image 71
Martijn Pieters Avatar answered Sep 07 '25 17:09

Martijn Pieters