admin管理员组文章数量:1303404
I've been creating lambda@edge functions to do various actions on a viewer request
event from cloudfront.
Most examples I can find seem to use callbacks but I wanted to use the async/await pattern instead, so I changed the handler function from:
exports.handler = (event, context, callback) => {
to:
exports.handler = async (event, context) => {
Examples using callbacks are returning errors via the first parameter in the callback such as:
callback(new Error("invalid token"));
My question is, with the async way is this code exactly equivalent?
throw new Error("invalid token")
As an aside, I'm not sure returning an error is the best approach anyway, and returning a response object with correct http code might be better.
I've been creating lambda@edge functions to do various actions on a viewer request
event from cloudfront.
Most examples I can find seem to use callbacks but I wanted to use the async/await pattern instead, so I changed the handler function from:
exports.handler = (event, context, callback) => {
to:
exports.handler = async (event, context) => {
Examples using callbacks are returning errors via the first parameter in the callback such as:
callback(new Error("invalid token"));
My question is, with the async way is this code exactly equivalent?
throw new Error("invalid token")
As an aside, I'm not sure returning an error is the best approach anyway, and returning a response object with correct http code might be better.
Share Improve this question asked Feb 4 at 14:58 OmironOmiron 3775 silver badges19 bronze badges2 Answers
Reset to default 0Yes, from the documentation, async functions always return a promise so throw new Error("invalid token")
exception would be wrapped into a promise and returned, similar to callback
returning error.
Alternatively, you can use Promise.reject() to manually wrap the error into a promise
return Promise.reject(new Error("invalid token"));
Here's a concise, point-wise explanation:
Async/Await vs Callback Differences:
- Callback method: callback(new Error("invalid token"))
- Async method: throw new Error("invalid token")
- They are NOT exactly equivalent
Key Differences:
- Throwing an error stops Lambda execution
- Callback allows more controlled error handling
- Throwing prevents sending a proper response to CloudFront
Best Practices:
- Return response objects instead of throwing errors
- Include appropriate HTTP status codes
- Provide meaningful error messages
- Log errors for debugging
Recommended Pattern:
exports.handler = async (event, context) => {
try {
// Normal processing
if (error_condition) {
return {
status: '403',
body: JSON.stringify({ message: 'Error details' })
};
}
} catch (error) {
// Catch unexpected errors
return {
status: '500',
body: JSON.stringify({ message: 'Server error' })
};
}
};
Benefits of This Approach:
- More control over responses
- Proper error handling
- CloudFront-friendly error management.
本文标签: nodejsLambdaedge error handling with asyncawait (nodejs)Stack Overflow
版权声明:本文标题:node.js - Lambda@edge error handling with asyncawait (nodejs) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741757497a2396201.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论