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 badges
Add a comment  | 

2 Answers 2

Reset to default 0

Yes, 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