2017-06-29 5 views
1

Ich bekomme den folgenden Fehler bei der Verwendung boto3 mit Amazon SNS. Ich möchte nur die InvalidParameterException abfangen. Wie kann ich das Gleiche tun?Python: Catch Third Party Bibliothek Ausnahmen

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. 

Wenn ich versuche, InvalidParameterException zu fangen, es zeigt

NameError: global name 'InvalidParameterException' is not defined 

Ich habe botocore importiert. Jetzt, wenn ich versuche, botorcore.errorfactory.InvalidParameterException zu fangen, zeigt es an.

AttributeError: 'module' object has no attribute 'InvalidParameterException' 

Antwort

1

Die Bibliothek botocore generiert mehrere Ausnahmen von einer Basisklasse. Fangen Sie die Basisklasse:

from botocore.exceptions import ClientError 

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

die ClientError().response['Error']['Code'] Inspizieren zu variieren, wie Sie die Ausnahme behandeln und nur raise wieder, wenn Sie einen bestimmten Fehlertyp ignoriert werden sollen. Siehe die Error Handling documentation.

Verwandte Themen