-
Notifications
You must be signed in to change notification settings - Fork 2
Handling
handle(next: Function, shouldThrowOnUnhandled: boolean)
should be the entry point of all your lambdas. It will handle any kind of event from AWS.
Handle takes 2 parameters: next
, which is your controller function, and shouldThrowOnUnhandled
that decides what to do when an unhandled event happens.
Request and response body are automatically JSON encoded.
If the request body is invalid, it will return a HTTP 400 Bad Request
response.
POST
requests always get a 201 status code when no error happened. All other requests are either 200 or 204, depending on whether the body is empty or not.
In case of errors, see below.
All errors are caught by the handle function. The type of the errors is defined by the name
property on the error thrown.
If you throw an error in your controller, you can specify the HTTP status code by following this structure:
throw {
statusCode: number,
body: any,
};
If the error is a ValidationError
, the HTTP status code returned is a 422. The error will have the following structure:
{
"data": [
{
"message": "\"email\" is required",
"path": [
"email"
],
"type": "any.required",
"context": {
"key": "email",
"label": "email"
}
}
]
}
The data entries are the error messages returned by Joi. See Validation for more info.
export const handler = handle(async (event: APIGatewayEvent): Promise<string> => {
return 'hello world';
});