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

2024-05-04 13:38:33 发布

您现在位置:Python中文网/ 问答频道 /正文

下面的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

我得到的是:

^{pr2}$

我需要从e中可靠地提取message字符串,例如'Item size has exceeded the maximum allowed size'。我该怎么做?在


Tags: 代码awsmessagesizeresponsebatch对话conn
2条回答

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

以下是JSONResponseError(超超类的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得到“Item size has exceeded…”。在

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

^{pr2}$

以e.body为例,你会得到一个字典的错误。在

示例: {u“消息”:u“条件请求失败”,u“uu类型”:u'com.amazonaws.dynamodb网站.v20120810\ConditionalCheckFailedException'}

从这很容易你会得到信息。在

相关问题 更多 >