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

时间:2014-01-14 15:08:26

标签: 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"

    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'}'

我需要以某种方式可靠地从message中提取'Item size has exceeded the maximum allowed size'字符串,例如e。我该怎么办?

2 个答案:

答案 0 :(得分:1)

我假设您正在使用boto访问DynamoDB。

以下是JSONResponseError({1}} DynamoDBValidationError方法的超级类:

__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

答案 1 :(得分:0)

拿e.body,你会得到错误的字典。

例如: {u'message':你'条件请求失败',''__ type':u'com.amazonaws.dynamodb.v20120810#ConditionalCheckFailedException'}

很容易从中获得信息。