I wanted the error from Lambda to be the correct error 500, after doing a lot of research, came up with the following, which works:
At LAMBDA
For a good answer, I come back as shown below:
exports.handler = (event, context, callback) => { // .. var someData1 = { data: { httpStatusCode: 200, details: [ { prodId: "123", prodName: "Product 1" }, { "more": "213", "moreDetails": "Product 2" } ] } }; return callback(null, someData1); }
For a bad answer returning as below
exports.handler = (event, context, callback) => { // .. var someError1 = { error: { httpStatusCode: 500, details: [ { code: "ProductNotFound", message: "Product not found in Cart", description: "Product should be present after checkout, but not found in Cart", source: "/data/attributes/product" }, { code: "PasswordConfirmPasswordDoesntMatch", message: "Password and password confirmation do not match.", description: "Password and password confirmation must match for registration to succeed.", source: "/data/attributes/password", } ] } }; return callback(new Error(JSON.stringify(someError1))); }
In the API
For a GET METHOD, say GET / res1 / service1:
Through Method Response > Add Response, added 3 responses: - 200 - 300 - 400
Then
Through 'Integration Response' > 'Add integration response', create a Regex for 400 errors (client error): Lambda Error Regex .*"httpStatusCode":.*4.* 'Body Mapping Templates' > Add mapping template as: Content-Type application/json Template text box* $input.path('$.errorMessage') Similarly, create a Regex for 500 errors (server error): Lambda Error Regex .*"httpStatusCode":.*5.* 'Body Mapping Templates' > Add mapping template as: Content-Type application/json Template text box* $input.path('$.errorMessage')
Now post / res 1 / service1, remove the published url that is associated with the above lambda
When the REST (or Postman) advanced client client is used, you will see appropriate http codes, such as a server error (500) or 400, instead of 200 HTTP response codes for all requests that were specified in "httpStatusCode".
In the "Dashboard" API, in the Gateway API, we can see the http status codes, as shown below:
