How can I import the boto3 ssm ParameterNotFound exception?

北城以北 提交于 2019-12-05 12:28:23

问题


I would like to import the exception that occurs when a boto3 ssm parameter is not found with get_parameter. I'm trying to add some extra ssm functionality to the moto library, but I am stumped at this point.

>>> import boto3
>>> ssm = boto3.client('ssm')
>>> try:
        ssm.get_parameter(Name='not_found')
    except Exception as e:
        print(type(e))
<class 'botocore.errorfactory.ParameterNotFound'>
>>> from botocore.errorfactory import ParameterNotFound
ImportError: cannot import name 'ParameterNotFound'
>>> import botocore.errorfactory.ParameterNotFound
ModuleNotFoundError: No module named 'botocore.errorfactory.ParameterNotFound'; 'botocore.errorfactory' is not a package

However, the Exception cannot be imported, and does not appear to exist in the botocore code. How can I import this exception?


回答1:


From Botocore Error Handling

import boto3
from botocore.exceptions import ClientError

ssm = boto3.client('ssm')
try:
    ssm.get_parameter(Name='not_found')
except ClientError as e:
    print e.response['Error']['Code']



回答2:


mc = boto3.client('ssm')
try:
  ...
except mc.exceptions.ParameterNotFound:
  ...


来源:https://stackoverflow.com/questions/46063138/how-can-i-import-the-boto3-ssm-parameternotfound-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!