@keboola/serverless-request-handler
v2.1.4
Published
Wrapper for lambda functions creating unified response for error states
Downloads
3
Keywords
Readme
serverless-request-handler
Wrapper for AWS Lambda functions with API Gateway creating unified response for error states.
By default the handler adds 'Access-Control-Allow-Origin': '*'
and 'Content-Type': 'application/json; charset=utf-8'
headers to the response.
Installation
- Install npm package:
yarn add @keboola/serverless-request-handler
- Wrap each of your lambda handlers with this code:
const { RequestHandler } = require('@keboola/serverless-request-handler');
module.exports.handler = (event, context, callback) => RequestHandler.handler(() => {
const promise = new Promise(res => res());
const code = 204;
return RequestHandler.responsePromise(promise, event, context, callback, code);
}, event, context, callback);
RequestHandler.handler()
catches uncaught exceptionsRequestHandler.responsePromise()
catches rejected promises and formats output for resolved promise chain
You can also return status code and headers in the resolved promise:
module.exports.handler = (event, context, callback) => RequestHandler.handler(() => {
const promise = new Promise((res) => {
// ...
res({
headers: { 'Access-Control-Allow-Headers': 'Token' },
body: {
message: 'Resource was created',
},
statusCode: 201,
});
});
return RequestHandler.responsePromise(promise, event, context, callback);
}, event, context, callback);
Request logging
Each request is logged to Cloudwatch in this json format:
{
"event": {
"requestId": "4d4f1e1e-ca29-11e7-87c4-2fcc57c44b2d",
"apiRequestId": "369391fb-ca29-11e7-90b1-95afd12f3597",
"function": "developer-portal-prod-users",
"httpMethod": "GET",
"path": "/users/[email protected]"
},
"statusCode": 200
}
User Errors
If you want to return a user error, use UserError
class which is handled by the handler automatically and the output is formatted in appropriate way. The class offer several static functions as shortcuts for most common user errors:
badRequest(msg = 'Bad Request')
- for 400 errorsunauthorized(msg = 'Unauthorized')
- for 401 errorsnotFound(msg = 'Not Found')
- for 404 errorsunauthorized(msg = 'Unauthorized')
- for 401 errorsunprocessable(msg = 'Unprocessable')
- for 422 errorserror(msg = 'Error', code = 400)
- for other errors
Example:
const { UserError } = require('@keboola/serverless-request-handler');
if (!token) {
throw UserError.unauthorized('Token is missing');
}
User then gets such response with status code 401:
{
"errorMessage": "Token is missing",
"errorType": "Unauthorized",
"requestId": "4d4f1e1e-ca29-11e7-87c4-2fcc57c44b2d"
}
And CloudWatch log of the request is enriched with error
field containing the error message.