【问题标题】:How to catch exceptions from botocore如何从 botocore 捕获异常
【发布时间】:2020-01-19 21:04:20
【问题描述】:

我在使用boto3Amazon SNS 时遇到以下错误。我只想抓住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.

如果我尝试捕捉InvalidParameterException,它会显示

NameError: global name 'InvalidParameterException' is not defined

我已导入botocore。现在,如果我尝试捕捉 botorcore.errorfactory.InvalidParameterException 它会显示。

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

【问题讨论】:

    标签: python python-2.7 exception botocore


    【解决方案1】:

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

    from botocore.exceptions import ClientError
    
    try:    
        ...
    except ClientError as e:
        ...
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-07-14
      • 1970-01-01
      • 2019-05-20
      • 1970-01-01
      • 1970-01-01
      • 2011-09-26
      相关资源
      最近更新 更多