首页 > 解决方案 > 如何使用 Python 处理 API Gateway 中的自定义 Lambda 错误

问题描述

在我的应用程序中,我想处理自定义错误,例如 500,403,201,400 错误,我在 nodejs 中尝试过它工作正常,但我正在使用 python,所以我没有在 python 中找到任何解决方案来处理 http 状态代码,所以有什么办法可以处理它们对我有很大帮助。

var myErrorObj = {
    status : 500,
    errorType : "InternalServerError",
    httpStatus : 233,
    requestId : context.awsRequestId,
    trace : {
        "function": "abc()",
        "line": 123,
        "file": "abc.js"
    }
};
const responseInvoke = (eventData) => {
    return lambda.invoke({
        FunctionName: 'test777',
        Payload: JSON.stringify({"eventData":event}) // pass param
    }).promise();
};

responseInvoke(event)
.then(res => {
    console.log('*** response *** ', res);
    const parsedRes = JSON.parse(res.Payload);
    if(parsedRes.status === 'success') {
        callback(null, res.Payload);
    } else {
        context.fail(JSON.stringify(myErrorObj));
    }
})
.catch(err => {
    console.log(err);
    context.fail(JSON.stringify(myErrorObj));
});    

这是我在 python lambda 中从 python lambda 调用到 nodejs lambda 的代码,我根据我向 API 网关发送响应的消息向 nodejs lambda 返回成功(或)失败消息。

标签: python-3.xamazon-web-services

解决方案


推荐阅读