-
Notifications
You must be signed in to change notification settings - Fork 116
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Async handlers produce unhandled promise rejection warnings #43
Comments
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: exports.handler = async (event, context, callback) => {
try {
// your logic here
callback(null, result);
} catch (err) {
callback(err);
}
}; 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 |
I am also experiencing the same thing with my async callback. Even with the
I have all my |
thanks @mmiller42, i actually didnt know that :) hi @i8ramin - gonna close this as seems more to do with AWS lambda syntax than this |
@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: import fetch from "node-fetch";
exports.handler = async function(event, context, callback) {
try {
const response = await fetch("https://api.chucknorris.io/jokes/random");
const data = await response.json();
callback(null, {
statusCode: 200,
body: data.value
});
} catch (err) {
console.error(err);
}
}; Log:
|
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 Will have a go at fixing tonight. |
@nunoarruda at first I was seeing the same error, but can't reproduce it, so my assumptions were incorrect. Sorry for the noise. |
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 |
@sw-yx Shawn, I don't think we should be using callback with the two handler types above in the previous comment, but I was hoping someone at Netlify could confirm this for me. |
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 |
Ok, so maybe we can come up with a solution based on a check that spits out a warning.
|
ah i see @talves you want a nicer error message? sure I could do that. whats a nice error message? something like this? // ***Checking for a callback without err AND without lambdaResponse***
if (!lambdaResponse) {
throw new Error('No response detected from your handler lambda. You may have forgotten to call `callback` or return a Promise.')
}
if (!lambdaResponse.statusCode) {
throw new Error('No statusCode detected in your handler lambda. You may have forgotten to return an object with a defined statusCode (usually 200 for success) in your `callback`.')
} |
Yeah, those may work. Do we need to check for a pending response before we throw the error. Let me test this out because it would end up throwing the same Warning as above. Maybe we could just put a |
not sure what you mean by "throwing the same error" but i'll leave this open if you wanna send in a PR :) |
It might take me awhile to get to, but yeah, I will take a look. Also, I did a promise example for the CRA netlify/create-react-app-lambda#17 |
@sw-yx I confirmed my statement about the warning when you use
Throwing the error without handling the response gives the warning but also returns the error message we throw. I will have to look at this when I get time. All in all, this should be a confirmation that you CANNOT return a promise to the handler and use the callback together. |
soo ok tell me what to do |
We need to test 2 conditions at minimum:
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. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Any progress on this one ? 😄 |
Same problem and resolved by removing |
Test Repo
I'm getting warnings about unhandled promise rejection when using async handler functions like below.
I've also tried using
Promise.Resolve()
with the response object.My understanding is that either should work with node 8.10.x lambda instances but the netlify-lambda serve console produces the following warning:
The text was updated successfully, but these errors were encountered: