小编ash*_*han的帖子

在日志记录函数中使用惰性%格式 pylint 错误消息

我有一个 python 函数,如下所示,当启用 pylint 进行代码扫描时,它会抛出一个惰性格式错误。

def modify_response(data):
    try:
        response = {}
        response["User_ID"] = data[0]["User_ID"]["S"]
        response["Triggered_Timestamp"] = data[0]["Triggered_Timestamp"]["S"]
        return response
    except Exception as e:
        logging.exception("ModifyResponseError: {}".format(e))
        raise ModifyResponseError(json.dumps({"httpStatus": 501,"message": internal_error_message}))
Run Code Online (Sandbox Code Playgroud)

python error-handling formatting logging pylint

17
推荐指数
1
解决办法
2万
查看次数

考虑使用 'from' 关键字 pylint 建议明确重新加注

我有一个小的 python 代码,我在其中使用了异常处理。

def handler(event):
    try:
        client = boto3.client('dynamodb')
        response = client.scan(TableName=os.environ["datapipeline_table"])
        return response
    except Exception as error:
        logging.exception("GetPipelinesError: %s",json.dumps(error))
        raise GetPipelinesError(json.dumps({"httpStatus": 400, "message": "Unable to fetch Pipelines"}))

class GetPipelinesError(Exception):
    pass
Run Code Online (Sandbox Code Playgroud)

pylint 警告给了我“考虑使用 'from' 关键字明确重新提高”。我很少看到其他帖子,他们使用 from 并引发错误。我做了这样的修改

except Exception as GetPipelinesError:
    logging.exception("GetPipelinesError: %s",json.dumps(GetPipelinesError))
    raise json.dumps({"httpStatus": 400, "message": "Unable to fetch Pipelines"}) from GetPipelinesError
Run Code Online (Sandbox Code Playgroud)

这是正确的做法吗?

python error-handling exception pylint raise

2
推荐指数
1
解决办法
1117
查看次数

标签 统计

error-handling ×2

pylint ×2

python ×2

exception ×1

formatting ×1

logging ×1

raise ×1