当我的lambda函数返回对象时,API网关的响应映射

发布于 2025-02-12 00:11:46 字数 1569 浏览 1 评论 0原文

假设我有一个lambda函数,该函数返回对象(单词“对象”是这里的关键字):

module.exports = {
    handler: async (event, context, callback) => {
        const rnd = Math.random();
        if (rnd > 0.33) {
            //Successfull response
            callback(null, { att: "value" });
        }
        else if (rnd > 0.66) {
            //Error response
            const err = new Error("Error message");
            err.name = "ITEM_NOT_FOUND";
            //TODO: return the err object (somehow)
        }
        else {
            //Error response
            const err = new Error("Error message");
            err.name = "GENERIC_ERROR_CODE";
            //TODO: return the err object (somehow)
        }
    }
}

然后,我想将此lambda函数集成到API网关条目中。为此,我需要至少将两个“集成响应”映射到返回对象通过正则匹配中的HTTP状态代码(200、400,...)。

我想要的是,当我的lambda函数失败(返回错误对象)时,API网关应以 404 http状态代码回复,如果错误代码为item_not_found and <强> 400 否则。当然,如果lambda函数没有失败,则HTTP状态代码应为 200

不要浪费您的时间,我知道我可以像这样返回我的错误对象:

context.fail(JSON.stringify({code: "ITEM_NOT_FOUND", message: "Error message"}));

然后lambda函数将返回这样的东西:

{
  "errorType": "string",
  "errorMessage": "{\"code\":\"ITEM_NOT_FOUND\",\"messaage\":\"Error message\"}",
  "trace": []
}

但这不是我所追求的我抓住错误消息,我必须解析错误errormessage以获取实际错误对象。我希望有一种方法可以避免这种情况,而同时可以提出API网关映射以检测错误并返回适当的状态代码。

Let's say that I have a lambda function that returns an object (the word "object" is the keyword here):

module.exports = {
    handler: async (event, context, callback) => {
        const rnd = Math.random();
        if (rnd > 0.33) {
            //Successfull response
            callback(null, { att: "value" });
        }
        else if (rnd > 0.66) {
            //Error response
            const err = new Error("Error message");
            err.name = "ITEM_NOT_FOUND";
            //TODO: return the err object (somehow)
        }
        else {
            //Error response
            const err = new Error("Error message");
            err.name = "GENERIC_ERROR_CODE";
            //TODO: return the err object (somehow)
        }
    }
}

And then, I want to integrate this lambda function into an API Gateway entry. For that, I'll need to have at least two "Integration Response" mapping the return object into an HTTP status code (200, 400, ...) through a regex matching.

What I want is that when my lambda function fails (returns an error object), API Gateway should respond back with a 404 HTTP status code if the error code is ITEM_NOT_FOUND and 400 otherwise. Of course, if the lambda function does not fail, the HTTP status code should be 200.

Not to waste your time, I know that I can return my error object like this:

context.fail(JSON.stringify({code: "ITEM_NOT_FOUND", message: "Error message"}));

And then the lambda function will return something like this:

{
  "errorType": "string",
  "errorMessage": "{\"code\":\"ITEM_NOT_FOUND\",\"messaage\":\"Error message\"}",
  "trace": []
}

But this is not what I'm after since if I call this lambda function using AWS SDK, when I catch the error message, I'll have to parse the error errorMessage to get the actual error object. I was hoping there's a way to avoid that while at the same time be able to come up with the API Gateway mapping to detect the error and return the appropriate status code.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文