gpt4 book ai didi

python - 如何从AWS DynamoDB python异常中提取异常消息?

转载 作者:太空宇宙 更新时间:2023-11-03 18:38:18 24 4
gpt4 key购买 nike

我有以下 Python 代码块与 AWS 上的 DynamoDB 通信:

try:
response = conn.batch_write_item(batch_list)
except Exception ,e:
try:
mess = e.message
except:
mess = "NOMESS"

try:
earg0 = e.args[0]
except:
earg0 = "NOEARG0"

try:
stre = str(e)
except:
stre = "NOSTRE"

print "mess = '%s'" % mess
print "earg0 = '%s'" % earg0
print "stre = '%s'" % stre

我得到的是这样的:

mess = ''
earg0 = 'NOEARG0'
stre = 'DynamoDBValidationError: 400 Bad Request {'message': 'Item size has exceeded the maximum allowed size', '__type': 'com.amazon.coral.validate#ValidationException'}'

我需要以某种方式可靠地从 e 中提取消息字符串,例如“项目大小已超过允许的最大大小”。我该怎么做?

最佳答案

我假设您使用 boto 访问 DynamoDB。

这是 JSONResponseError(DynamoDBValidationError 的超父类(super class))__init__ 方法:

self.status = status
self.reason = reason
self.body = body
if self.body:
self.error_message = self.body.get('message', None)
self.error_code = self.body.get('__type', None)
if self.error_code:
self.error_code = self.error_code.split('#')[-1]

疯狂猜测:我会使用 e.error_message 来获取“项目大小已超过...”。

您还可以打印e的所有属性(及其值):

for attr in dir(e): 
print "e[%r] = '''%s'''" % (attr, getattr(e, attr))

关于python - 如何从AWS DynamoDB python异常中提取异常消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21116878/

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