【问题标题】:Node Promise and AWS Lambda error handlingNode Promise 和 AWS Lambda 错误处理
【发布时间】:2017-03-21 04:15:00
【问题描述】:

我正在我的节点 Lambda 中使用本机承诺进行一些错误处理。出于某种原因,我的 promise.reject 永远不会触发。我专门发送了一个错误的 url 以使其失败,但 AWS 发送回 502“Bad Gateway”。这大概是 AWS 处理内部服务器问题的方式,这很好,但我想发回拒绝原因。我做错了什么?

承诺

function parseData(data) {
    url = 'https://notarealurl';

    return new Promise((resolve, reject) => {
        https.get(url, (res) => {
            let body = '';
            res.on('data', (chunk) => {
                body += chunk;
            })
            .on('end', () => {
                resolve(body);
            })
            //this reject will trigger when there is an error with the request
            .on('error', (err) => {
                const rejectReason = {
                    statusCode: 500,
                    body: 'Internal Server Error: ' + err
                };
                reject(rejectReason)
            });
        })
    });
}

我的处理程序:

function handler(event, context) {
    parseData(event.queryStringParameters)
    .then((result) => {
        const parsed = JSON.parse(result);
        //handle error message returned in response
        if (parsed.error) {
            const oAuthError = 'Authentication Error';
            let error = {
                headers: {"Content-Type": "application/json"},
                body: JSON.stringify(parsed.error)
            };
            if(parsed.error.type === oAuthError) {
                error.statusCode = 401;
                context.succeed(error);
            } else {
                error.statusCode = 404;
                return context.succeed(error);
            }
        } else {
            return context.succeed({
                statusCode: 302,
                headers: "Success"
              });
        }
    })
    .catch((reason) => {
        console.log('Promise rejected!!!!', reason)
    })
}

出现的 AWS 错误是什么,而不是我的 promise.reject errorReason?

【问题讨论】:

  • 如果是 401 和 404,你应该使用 context.fail 而不是 context.success。
  • @notionquest 如果您返回 context.fail,API Gateway 会将其解释为服务器出现问题并抛出 502。由于请求成功但响应包含错误,因此这是有道理的,根据我对上一个问题的回答:stackoverflow.com/questions/42847945/…

标签: javascript node.js amazon-web-services lambda es6-promise


【解决方案1】:

我发现了我的问题。我在 get 中有我的 .on('error') 。改成如下代码,效果很好:

function parseData(data) {
    url = 'https://notarealurl';

    return new Promise((resolve, reject) => {
        https.get(url, (res) => {
            let body = '';
            res.on('data', (chunk) => {
                body += chunk;
            })
            .on('end', () => {
                resolve(body);
            })
            //this reject will trigger when there is an error with the request
        })
        .on('error', (err) => {
                const rejectReason = {
                    statusCode: 500,
                    body: 'Internal Server Error: ' + err
                };
                reject(rejectReason)
            });
    });
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-03
    • 1970-01-01
    • 2014-08-22
    • 2017-06-23
    • 1970-01-01
    • 2021-01-23
    相关资源
    最近更新 更多