AWS API Gateway:未获取用于错误的正则表达式 [英] AWS API Gateway: Regex for error is not picked up

查看:119
本文介绍了AWS API Gateway:未获取用于错误的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个绑定到AWS API网关的Lambda函数.当我用/foo/bar之类的东西调用它,而bar不存在时,我的函数返回(正确)如下信息:

{
  "code": 404,
  "message": "bar not found"
}

我现在也希望它的状态码也为404,但是按照我在网上可以找到的教程,到目前为止,我还没有成功.我做了什么:

我为404创建了一个方法响应,如下所示:

在集成响应中,我设置了以下内容:

据我了解,这应该在响应文档中出现404时将代码设置为404,但我仍然会得到200.

我想念什么?

解决方案

我进行了更多研究,发现API网关对于期望错误消息的位置以及如何清除错误消息非常挑剔.

它被深深掩埋,在文档,其中认为errorMessage属性与Lambda错误正则表达式以及

然后我设置了Lambda错误正则表达式404,它起作用了.

我尝试的下一件事情是:

new Exception("foo 404 bar") |> raise

使用正则表达式.*404.*仍然有效.

现在事情来了:我试图发出一个JSON对象作为错误,但是我没有在C#或F#中找到任何让我执行此操作的东西,所以我想到了以下内容:

type Error = {
    code: int
    message: string
}

...

new Exception({ code = 404; message = name |> sprintf "%s not found" } |> JsonConvert.SerializeObject) |> raise

繁荣,我又得到了200.

所以我由此得出结论,AWS不喜欢errorMessage属性中的字符串化JSON,所以现在我只输出一个简单的字符串,如下所示:

new Exception(name |> sprintf "404: %s not found") |> raise

并使用正则表达式404:.*,现在可以使用了.这意味着,我必须以某种方式使用API​​网关中的映射来构造所需的输出对象.

这很不方便,很容易绊倒...

I have a Lambda function tied to the AWS API gateway. When I call it with something like /foo/bar and bar does not exist, my function returns (correctly) something like:

{
  "code": 404,
  "message": "bar not found"
}

I now want the status code of this to be 404, too, but following the tutorials I could find on the net, I had no success so far. What I did:

I created a method response for 404 that looks like this:

And in the integration response, I set the following:

To my understanding, this should set the code to 404 when 404 appears in the response document, but I still get 200.

What am I missing?

解决方案

I investigated some more and found out that the API gateway is very picky about where it expects its error messages, and how I have to get them out.

It is deeply buried and not easily found in the documentation that the errorMessage property is considered for matching against the Lambda Error Regex, and the first answer to this post states you have to throw an exception when using Java 8. I use .net for my Lambda function, so I did this very simple thing:

new Exception("404") |> raise

then I set up the Lambda Error Regex 404 and it worked.

Next thing I tried was:

new Exception("foo 404 bar") |> raise

With the regex .*404.* it still worked.

Now comes the thing: I tried to emit a JSON object as the error, but I found nothing in C# or F# to let me do this, so I came up with the following:

type Error = {
    code: int
    message: string
}

...

new Exception({ code = 404; message = name |> sprintf "%s not found" } |> JsonConvert.SerializeObject) |> raise

And boom, I got 200 again.

So I conclude from this, AWS doesn't like stringified JSON in the errorMessage property, so I now just output a simple string like this:

new Exception(name |> sprintf "404: %s not found") |> raise

and using the regex 404:.*, it now works. That means, I somehow have to construct my desired output object using the mappings in the API gateway.

This is quite inconvenient and something easy to trip over...

这篇关于AWS API Gateway:未获取用于错误的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆