admin管理员组文章数量:1355570
I'm trying to invoke a lambda function from an external node.js app (i.e not on AWS). I used code samples from the AWS documenattion and it works great for nominal cases. However, in the case of an error, it never gets processed properly by my calling function.
My invocation code is as follow:
// AWS.config before
var pullParams = {
FunctionName : 'myFunctionName',
InvocationType : 'RequestResponse',
LogType : 'None',
Payload : JSON.stringify({
"myParam" : params
})
};
var lambda = new AWS.Lambda();
lambda.invoke(pullParams, function(error, data) {
console.log("error value: " + error);
if (error) {
console.log("Error invoking AWS " + error);
// process error
} else {
// process payload
}
});
I'm trying to invoke a lambda function from an external node.js app (i.e not on AWS). I used code samples from the AWS documenattion and it works great for nominal cases. However, in the case of an error, it never gets processed properly by my calling function.
My invocation code is as follow:
// AWS.config before
var pullParams = {
FunctionName : 'myFunctionName',
InvocationType : 'RequestResponse',
LogType : 'None',
Payload : JSON.stringify({
"myParam" : params
})
};
var lambda = new AWS.Lambda();
lambda.invoke(pullParams, function(error, data) {
console.log("error value: " + error);
if (error) {
console.log("Error invoking AWS " + error);
// process error
} else {
// process payload
}
});
In my lambda function, I raise an error if no params are provided and provide an error message
exports.handler = (event, context, callback) => {
var params = event.myParam;
if (!params) {
var error = new Error("Appropriate error message");
callback(error);
// In Node ≥8, could also be expressed making handler `async` and `throw`ing here
}
else {
// do normal processing and create payload
callback(null, "Payload");
}
}
However, in the invoke
callback, error
is always null (even when the lambda goes through the error code path), and when going through the error code path, then data
contains errorMessage, errorType, stackTrace
keys.
What am I missing here? Shouldn't the invoke
binding of aws-sdk
populate error
rather than making me check for data.errorMessage
?!
-
My question is... does
err
only get set when it fails to invoke it, and not when you return an error response? that seems to be the behavior you're seeing. I haven't used the javascript sdk to invoke lambda functions yet so i'm not sure of what's intended. The docs are somewhat unclear – Kevin B Commented Mar 8, 2017 at 23:06 -
1
Looking at the docs over here that seems to support the idea that returning an error from the function won't result in
err
being set and an error status code, and that the error you provide will be in the payload. – Kevin B Commented Mar 8, 2017 at 23:10 - 1 yes indeed; and it's not necessarily a bad way to do things; but the code samples such as link would be incorrect, hence my suspicion that this approach is wrong – Francis Limousy Commented Mar 8, 2017 at 23:22
-
Filed aws/aws-sdk-js issue #2134 - Lambda.invoke's
error
callback argument is never populated – Ronan Jouchet Commented Jul 10, 2018 at 18:58
3 Answers
Reset to default 3I can use a workaround such as testing on the payload received:
//if (invocationError){
if (data.FunctionError){ <== null if no error, "Handled" if an error is returned
This technically works but its different from every code snipplet I found around.
You can solve this by checking the data.Payload for an error message
var pullParams = {
FunctionName : 'myFunctionName',
InvocationType : 'RequestResponse',
LogType : 'None',
Payload : JSON.stringify({
"myParam" : params
})
};
var lambda = new AWS.Lambda();
lambda.invoke(pullParams, function(error, data) {
console.log("error value: " + error);
//data.Payload is returned as a string, to check it, turn into JSON object
let payload = JSON.parse(data.Payload);
if (error) {
console.log("Error invoking AWS " + error);
// process error
} else {
// process payload
// check for Lambda error
if(!payload.errorMessage) {
console.log(data.Payload);
} else {
//Lambda error
console.log(payload.errorMessage);
}
}
});
Use context.fail() instead of callback(err) to get handler failing
本文标签: javascriptWhy is AWSLambdainvoke error callback argument never populatedStack Overflow
版权声明:本文标题:javascript - Why is AWS.Lambda.invoke `error` callback argument never populated? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744046301a2581587.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论