javascript - Getting error string from Lambda invoked via AWS API
问题描述
Consider the following example Lambda function:
//get-account
exports.handler = (data, context, callback) => {
callback("Account not found");
};
Lambda would output this error like so:
{
"errorMessage": "Account not found"
}
This is exactly what I want. However, consider then calling this function via the AWS API
return new Promise((resolve, reject) => {
Lambda.invoke({
FunctionName: 'get-account',
InvocationType: "RequestResponse",
Payload: JSON.stringify({ account_id: account_id })
}, function(err, data) {
//only true if the invocation failed...
if(err) { return reject(err); }
//parse the payload. We will need it regardless of error/success
let response = JSON.parse(data.Payload);
//did the function throw an error?
if(data.FunctionError) {
//the error message will be [object Object], which is no good!
reject(response.errorMessage);
return;
}
//success!
resolve(response);
});
});
In this case, I find the error is always converted to the [object Object]
string. Lambda appears to take the error object from the callback (the error object it creates) and then wraps it in another error object. So ultimately Lambda.invoke is doing this:
{
errorMessage: {
errorMessage: "Account not found"
}
}
But instead of returning this built object, it returns,
{
errorMessage: '[object Object]'
}
Anyone see a way around this? I do NOT want to change how my Lambda function outputs errors. I only want to get the correct error from the Lambda invoke function. Is this simply not possible due to how Lambda.invoke()
wraps the error again?
解决方案
推荐阅读
- angular - 从其他组件Angular 10的按钮添加不同的类
- reactjs - React,如何将用户分配给按钮,以阻止用户?
- memory-management - Linux中中毒页面有什么用,调用check_poison_mem后出现页面损坏错误是什么意思?
- blender - Godot 没有正确导入 glTF 材质着色器
- php - 使用循环取消设置php
- javascript - 从 funcA 到 funcB 获取值 - 承诺
- angular - 无法访问 Cloud Firestore 后端。后端 > 10 秒内没有响应
- liquid - 机车 CMS:使用 Liquid 变量动态定位 content_types
- python - 如何在tkinter python中搜索用户输入文件是否存在
- java - 我可以用 Java 读取和处理这一行文件的最快方法是什么?