[英]Boto3 AWS API error responses for SSM
I am using a simple boto3 script to retrieve a parameter from SSM param store in my aws account.我正在使用一个简单的 boto3 脚本从我的 aws 帐户中的 SSM 参数存储中检索参数。 The python script looks like below:
python 脚本如下所示:
client = get_boto3_client('ssm', 'us-east-1')
try:
response = client.get_parameter(Name='my_param_name',WithDecryption=True)
except Exception as e:
logging.error("retrieve param error: {0}".format(e))
raise e
return response
If the given parameter is not available, I get a generic error in the response like below:如果给定的参数不可用,我会在响应中收到一个通用错误,如下所示:
An error occurred (ParameterNotFound) when calling the GetParameter operation: Parameter my_param_name not found.
I have verified method signature from boto3 ssm docs .我已经验证了boto3 ssm docs 的方法签名。 Related AWS API Docs confirms to return a 400 response when parameter does not exist in the param store.
相关的AWS API 文档确认在参数存储中不存在参数时返回 400 响应。
My question is that how do I verify if the exception caught in the response is actually a 400 status code so that I can handle it accordingly.我的问题是如何验证响应中捕获的异常是否实际上是 400 状态代码,以便我可以相应地处理它。
You can try catching client.exceptions.ParameterNotFound
:您可以尝试捕获
client.exceptions.ParameterNotFound
:
client = get_boto3_client('ssm', 'us-east-1')
try:
response = client.get_parameter(Name='my_param_name',WithDecryption=True)
except client.exceptions.ParameterNotFound:
logging.error("not found")
You can look at the status via response['Error']['Code'], but since there are multiple reasons for a 400, I would recommend a better approach:您可以通过 response['Error']['Code'] 查看状态,但由于 400 有多种原因,我建议使用更好的方法:
response = client.get_parameter(Name='my_param_name',WithDecryption=True)
if 'Parameters' not in response:
raise ValueError('Response did not contain parameters key')
else:
return response
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.