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...