0

I'm able to invoke a lambda function from another lambda function using aws-sdk invoke() function. I wrote both lambda functions. The lambda function I invoke, returns context.fail() in case of any error occurrence. I invoke the lambda function like this:

lambda
.invoke({
  FunctionName: functionName,
  InvocationType: "RequestResponse",
  Payload: JSON.stringify({
    body: { params },
  }),
})
.promise()
.then((res) => console.log(res))
.catch((error) => console.log(error));

when I call the lambda function by API Gateway context.fail() works fine and I get status code 400 as I declared in mapping template. but when I invoke the lambda function directly, I can not catch errors in the above code and it always resolve. How can I catch errors in above code? Thanks

amir
  • 13
  • 2
  • 1
    It won't throw an exception, you'll need to check the `FunctionError` property of the result and then do what you want with the error (in the `Payload` property). See https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/Lambda.html#invoke-property – 404 Jan 29 '21 at 13:20
  • That makes sense. but what if I want to use .promise() ? – amir Jan 29 '21 at 13:26
  • Not 100% sure as I never use Promises directly, but I imagine the `then(...)` would be where you check `res` for `FunctionError`, e.g. `.then(res => { if (res.FunctionError) then { /* res.Payload contains your error object, possibly as a buffer */ } else { /* all good */ }})` – 404 Jan 29 '21 at 13:48

0 Answers0