How can I import the boto3 ssm ParameterNotFound e

2020-07-02 08:18发布

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?

2条回答
干净又极端
2楼-- · 2020-07-02 09:09

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']
查看更多
Luminary・发光体
3楼-- · 2020-07-02 09:09
mc = boto3.client('ssm')
try:
  ...
except mc.exceptions.ParameterNotFound:
  ...
查看更多
登录 后发表回答