Configure API Gateway to return proper HTTP error codes - ActoKids/api GitHub Wiki
Note: This wiki assumes an existing API Gateway with at least one resource, one method, and integration with a Lambda function written in Node.js version 8.1 or higher. See part 1 of this wiki for info on adding a resource and method.
Part 1: Add a Method Response
- From the API Gateway console, select the appropriate API from the list of available APIs.
- From the list of available Resources, click to to select a Method (e.g. GET) from beneath one of the Resources (e.g /events).
- From the Method Execution Diagram on the right, click 'Method Response'.
- Click 'Add Response'
- Enter the number for the desired HTTP status code (e.g. 500 for a generic server error) and click the check mark button to confirm. Add additional status codes in the same manner, as needed.
Part 2: Configure Lambda function
- From the Lambda console, select the function that will be triggered by a given API Gateway Resource and Method (e.g the function triggered by a GET request on /events)
- In the Function code editor, find the code block that handles errors of the type defined in the Method Response. For example, an error related to connecting to DynamoDB should ultimately result in an HTTP status code of 500.
- Add a property to the event object, so that it can be referenced later in the Integration Response Mapping Template (see Part 3). For example, if the error should result in a status code of 500, you might modify the error object as follows:
error['statusCode'] = 500 - Use the static method, JSON.stringify() to parse the error object into a string before it is returned.
- If you are returning an error response directly, the code would look like
return JSON.stringify(error) - If you are using the handler's callback parameter, the code would look like
callback(JSON.stringify(error), null)
- If you are returning an error response directly, the code would look like
Part 3: Add an Integration Response
- From the API Gateway console, return to the Method Execution Diagram by clicking the Method name beneath the list of Resources.
- Click 'Integration Response'
- Click 'Add integration response'
- For 'Lambda Error Regex', enter a regular expression for the property that you added to the error object in Lambda (see part 2). For example, if you had added the property,
statusCode: 500, to the error object in your Lambda function, you would enter.*"statusCode":500.* - For 'Method response status', select the desired status code from the drop-down. Note: if the drop-down is empty, make sure you've completed Part 1 of this wiki.
- For 'Content handling', select 'Passthrough' from the drop-down
- Click the 'Save' Button
- Click the black arrow beside the newly-created integration response.
- Click the black arrow beside 'Mapping Templates'
- Click 'Add mapping template'
- For the 'Content-Type', enter 'application/json' and click the check-mark button. Note: This field will appear to be pre-filled, but it's not -- you have to type over the grey text.
- In the template window that appears, enter
$input.path('$.errorMessage') - Click the grey 'Save' button beneath the template window, AND click the blue 'Save' button above.
- Click the 'Actions' menu button and select 'Deploy API'. Choose a deployment stage and click 'Deploy'.