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

如何从AWS DynamoDB python异常中提取异常消息?,python,exception,message,amazon-dynamodb,Python,Exception,Message,Amazon Dynamodb,我有以下一段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"

我有以下一段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的超超类)
\uuuuu init\uuuu
方法:

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\u message
获取“项目大小已超过…”

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

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

以e.body为例,您将得到错误作为字典

例如: {u'message':u'The conditional request failed',u''u'type':u'com.amazonaws.dynamodb.v20120810#ConditionalCheckFailedException'}

从中你会很容易地得到信息