【问题标题】:AWS Lambda returns 502 when using Promise.all()AWS Lambda 在使用 Promise.all() 时返回 502
【发布时间】:2023-03-19 12:45:02
【问题描述】:

Endpoint 在每种情况下都返回 502。我认为我的响应结构正确,但我的端点仍然收到 502 Bad Gateway 错误。

当我不处理 Promise.all() 时,Responses 正在工作。

顺便说一句,这个函数的超时时间是 30 秒,但不会花那么长时间。

我有一个这样的处理程序:

module.exports.handler= async function (event, context, callback) {
  await Promise.all([promise1(), promise2()])
    .then((value) => {
      promise3(value[0], value[1])
        .then(() => {
          const response = {
            statusCode: 200,
            body: JSON.stringify({
              message: "Successful",
            }),
          };
          callback(null, response);
        })
        .catch((err) => {
          const response = {
            statusCode: 401,
            body: JSON.stringify({
              message: err,
            }),
          };
          callback(null, response);
        });
    })
    .catch((err) => {
      const response = {
        statusCode: 500,
        body: JSON.stringify({
          message: err,
        }),
      };
      callback(null, response);
    });
};

我从 API Gateway 的测试中得到的错误:

Sat Dec 05 09:24:08 UTC 2020 : Endpoint response body before transformations: null
Sat Dec 05 09:24:08 UTC 2020 : Execution failed due to configuration error: Malformed Lambda proxy 
response
Sat Dec 05 09:24:08 UTC 2020 : Method completed with status: 502

【问题讨论】:

    标签: node.js amazon-web-services rest aws-lambda serverless


    【解决方案1】:

    您应该坚持使用await.then,而不是同时使用两者。

    由于您的处理程序已经是async,您不妨使用await

    我在下面修改了您的代码,使其不再使用.then(未经测试)。

    module.exports.handler= async function (event, context, callback) {
        try
        {
            var values = await Promise.all([promise1(), promise2()]);
    
            try
            {
                await promise3(values[0], values[1]);
                const response = {
                    statusCode: 200,
                    body: JSON.stringify({
                        message: "Successful",
                    })
                };
                callback(null, response);
            }
            catch(err)
            {
                const response = {
                    statusCode: 401,
                    body: JSON.stringify({
                        message: err,
                    })
                };
                callback(null, response);
            }
        }
        catch(err) 
        {
            const response = {
                statusCode: 500,
                body: JSON.stringify({
                    message: err,
                })
            };
            callback(null, response);
        }
    }
    

    【讨论】:

    • 它工作正常,非常感谢。但是在您的代码中,您忘记在 200 状态代码案例中添加“callback(null, response)”。
    • 很高兴它有效。谢谢,我已经用那个回调编辑了
    • 离线部署函数在 2.5 秒内返回,但在在线它超时(30 秒)你知道为什么会这样吗?
    • 确定添加“context.callbackWaitsForEmptyEventLoop = false;”已解决的问题。
    猜你喜欢
    • 1970-01-01
    • 2018-11-28
    • 2019-09-15
    • 2017-06-15
    • 2018-09-23
    • 1970-01-01
    • 2015-10-23
    • 2020-11-02
    • 1970-01-01
    相关资源
    最近更新 更多