简体   繁体   中英

How exactly does Lambda Error Regex work for custom errors?

I'm aware about Lambda Proxy Integration, however I'm curious about Lambda Custom Proxy. To catch the custom error, I'm going to define regular expression inside Integration Response associated with following Lambda function:

exports.handler = (event, context, callback) => {        
    ...
    // Error caught here:
    var myErrorObj = {
        errorType : "InternalServerError",
        httpStatus : 500,
        requestId : context.awsRequestId,
        trace : {
            "function": "abc()",
            "line": 123,
            "file": "abc.js"
        }
    }
    callback(JSON.stringify(myErrorObj));
};

According to doc : "When a method of your API is integrated with the preceding Lambda function, API Gateway receives an integration response with the following payload"

{
    "errorMessage": "{\"errorType\":\"InternalServerError\",\"httpStatus\":500,\"requestId\":\"e5849002-39a0-11e7-a419-5bb5807c9fb2\",\"trace\":{\"function\":\"abc()\",\"line\":123,\"file\":\"abc.js\"}}"
}

Please note that there is a backslash inside httpStatus\\":500 (let's call this target), so logically Lambda Error Regex should be something like .*httpStatus\\\\":500.* , that is it should escape the backslash, so that it can catch that target. However, such regex doesn't catch the error, instead .*"httpStatus":500.* catches the error. The latter regex normally catches term httpStatus":500 . But this contradicts with the doc. In other words, it seems that Regular Expression of API gateway doesn't scan the string as it's explained by the docs? So, what does it exactly scan? What exactly happens in between?

{
    "errorMessage": "{\"errorType\":\"InternalServerError\",\"httpStatus\":500,\"requestId\":\"e5849002-39a0-11e7-a419-5bb5807c9fb2\",\"trace\":{\"function\":\"abc()\",\"line\":123,\"file\":\"abc.js\"}}"
}

This is the raw response payload that API Gateway receives from Lambda. That is a JSON object, containing your errorMessage string, which contains a JSON object (that you put there). Lambda has JSON.stringify'ed the whole thing a second time for the wire response, as it is designed to do.

It would be inappropriate to try to parse the payload directly, so it is automatically being decoded, once, removing the outer layer of JSON serialization so that your regular expression is applied to your error message string exactly as you handed it to the callback. The backslash-escaped quotes are an artifact of JSON-in-JSON, and they are removed when the first decoding step happens.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM