gpt4 book ai didi

python - 如何从 botocore 捕获异常

转载 作者:行者123 更新时间:2023-11-28 22:26:26 24 4
gpt4 key购买 nike

Amazon SNS 中使用 boto3 时出现以下错误。我只想捕获 InvalidParameterException,我该怎么做?

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.

如果我 try catch InvalidParameterException,它会显示

NameError: global name 'InvalidParameterException' is not defined

我已经导入了botocore。现在,如果我 try catch botorcore.errorfactory.InvalidParameterException,它会显示。

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

最佳答案

botocore 库从基类生成多个异常。捕获基类:

from botocore.exceptions import ClientError

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

检查 ClientError().response['Error']['Code'] 以改变处理异常的方式,如果需要,只需再次 raise忽略特定的错误类型。查看Error Handling documentation .

关于python - 如何从 botocore 捕获异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44828156/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com