netlify-lambda: Async handlers produce unhandled promise rejection warnings
I’m getting warnings about unhandled promise rejection when using async handler functions like below.
exports.handler = async (event, context, callback) => {
callback(null, {
statusCode: 200,
body: JSON.stringify({ success: true }),
});
}
I’ve also tried using Promise.Resolve() with the response object.
exports.handler = async (event, context, callback) => {
Promise.resolve({
statusCode: 200,
body: JSON.stringify({ success: true }),
});
}
My understanding is that either should work with node 8.10.x lambda instances but the netlify-lambda serve console produces the following warning:
UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'statusCode' of undefined
at callback (/Users/lloyd/dev/netlify-lambda-async-test/node_modules/netlify-lambda/lib/serve.js:22:42)
at /Users/lloyd/dev/netlify-lambda-async-test/node_modules/netlify-lambda/lib/serve.js:41:21
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:182:7)
(node:56861) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 24 (17 by maintainers)
When an async function throws an error, the function doesn’t throw the error on its invocation, it rejects the promise it returns. However, AWS uses callbacks, not promises, so it doesn’t read the return value of your function (which is the rejected promise).
You should try something like this:
Now you’re explicitly passing any error that occurs to the callback, where Netlify/Lambda can actually read the error.
Your second example won’t work, as Lambda doesn’t use promises – also, you have to make sure to
returna promise from a function if you want it to be handled (but again, Lambda won’t).@sw-yx can you reopen this? I’m having the same issue as @lloydh and @i8ramin but @mmiller42 proposed solution does not solve the problem.
Here’s my demo:
Log:
i think you are correct Tony. the docs indicate the same thing. Here’s Serverless calling it out as a mistake as well
i’m sadly far from a lambda expert. just trying to help maintain things as an employee with commit rights and a heavy user 😃 this thread has taught me something.
going to close this and add an example to CRAL
Using async/await
Returning a promise
I am not an AWS lambda expert, but this is the way I interpret using these new handlers in v8.10 Please let me know if this is the way we should be using these with Netlify functions. BTW, these are both working for me in a site on Netlify.
We need to test 2 conditions at minimum:
ansynchandler and returning the callback.I am not sure what to do, until I am sure that all entry points on the handler are tested because I do not want to introduce false messages to the developer.
I am also experiencing the same thing with my async callback. Even with the
try/catchblock. I put a console.log where the error is happening, and it looks like there are 2 requests happening for some reason. The first one succeeds, and the second one has an undefinedlambdaResponseI have all my
async/awaitfunctions surrounded bytry/catchblocks and callcallback(err)in thecatchblock.@talves, that fixes the error, thanks.
Try adding a body value with at least an empty string. I am curious if that is the type that is undefined.
Reference: https://github.com/netlify/netlify-lambda/blob/master/lib/serve.js#L29
From the examples and logs I’m guessing the promise rejection is within netlify-lambda, and specifically the logic behind determining if a response / callback is a
Promiseor not.Will have a go at fixing tonight.