AWS API Gateway:未获取用于错误的正则表达式 [英] AWS API Gateway: Regex for error is not picked up
问题描述
我有一个绑定到AWS API网关的Lambda函数.当我用/foo/bar
之类的东西调用它,而bar
不存在时,我的函数返回(正确)如下信息:
{
"code": 404,
"message": "bar not found"
}
我现在也希望它的状态码也为404,但是按照我在网上可以找到的教程,到目前为止,我还没有成功.我做了什么:
我为404创建了一个方法响应,如下所示:
在集成响应中,我设置了以下内容:
据我了解,这应该在响应文档中出现404
时将代码设置为404,但我仍然会得到200
.
我想念什么?
我进行了更多研究,发现API网关对于期望错误消息的位置以及如何清除错误消息非常挑剔.>
它被深深掩埋,在文档,其中认为 然后我设置了Lambda错误正则表达式 我尝试的下一件事情是: 使用正则表达式 现在事情来了:我试图发出一个JSON对象作为错误,但是我没有在C#或F#中找到任何让我执行此操作的东西,所以我想到了以下内容: 繁荣,我又得到了200. 所以我由此得出结论,AWS不喜欢 并使用正则表达式 这很不方便,很容易绊倒... I have a Lambda function tied to the AWS API gateway. When I call it with something like 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 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 then I set up the Lambda Error Regex Next thing I tried was: With the regex 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: And boom, I got 200 again. So I conclude from this, AWS doesn't like stringified JSON in the and using the regex This is quite inconvenient and something easy to trip over... 这篇关于AWS API Gateway:未获取用于错误的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!errorMessage
属性与Lambda错误正则表达式以及404
,它起作用了.new Exception("foo 404 bar") |> raise
.*404.*
仍然有效.type Error = {
code: int
message: string
}
...
new Exception({ code = 404; message = name |> sprintf "%s not found" } |> JsonConvert.SerializeObject) |> raise
errorMessage
属性中的字符串化JSON,所以现在我只输出一个简单的字符串,如下所示:new Exception(name |> sprintf "404: %s not found") |> raise
404:.*
,现在可以使用了.这意味着,我必须以某种方式使用API网关中的映射来构造所需的输出对象./foo/bar
and bar
does not exist, my function returns (correctly) something like:{
"code": 404,
"message": "bar not found"
}
404
appears in the response document, but I still get 200
.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
404
and it worked.new Exception("foo 404 bar") |> raise
.*404.*
it still worked.type Error = {
code: int
message: string
}
...
new Exception({ code = 404; message = name |> sprintf "%s not found" } |> JsonConvert.SerializeObject) |> raise
errorMessage
property, so I now just output a simple string like this:new Exception(name |> sprintf "404: %s not found") |> raise
404:.*
, it now works. That means, I somehow have to construct my desired output object using the mappings in the API gateway.