python-3.x - 如何使用 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 返回成功(或)失败消息。
解决方案
推荐阅读
- android - Android 警报管理器插件不会触发
- assembly - 如何使用汇编语言,使用 looz 指令在累加器体系结构中查找具有 n 个元素的数组的总和
- android - cardview android上的clicklistener
- rest - 有没有办法使用 REST API 提取所有受 Rally 风险影响的工作项?
- python-3.x - Tkinter Radiobutton:与图像一起删除点
- cookies - HTTP 请求:有什么方法可以共享保持通配符访问来源的 cookie?
- python - kivy中的图像按钮不简洁
- css - 尝试样式化与类的链接
- mongodb - 从 mongoose model.statics 函数中插入一条记录
- mysql - ORDER BY MONTH(1) 在 GROUP_CONCAT 作为名称之后?