@lambda-middleware/http-error-handler
Version:
An error handler middleware for AWS http lambda functions.
43 lines (33 loc) • 2.45 kB
Markdown
//badge.fury.io/js/%40lambda-middleware%2Fhttp-error-handler.svg)](https://npmjs.org/package/@lambda-middleware/http-error-handler)
[](https://npmjs.org/package/@lambda-middleware/http-error-handler)
[](https://github.com/dbartholomae/lambda-middleware/issues)
[](https://github.com/visionmedia/debug#readme)
[](https://github.com/dbartholomae/lambda-middleware/actions?query=workflow%3A.github%2Fworkflows%2Fbuild.yml)
[](https://codecov.io/gh/dbartholomae/lambda-middleware)
[](https://david-dm.org/dbartholomae/lambda-middleware)
[](https://david-dm.org/dbartholomae/lambda-middleware?type=dev)
An error handler middleware for AWS http lambda functions, compatible with [http-errors](https://www.npmjs.com/package/http-errors).
This middleware is part of the [lambda middleware series](https://dbartholomae.github.io/lambda-middleware/). It can be used independently.
```typescript
import { errorHandler } from "@lambda-middleware/errorHandler";
import { APIGatewayEvent, APIGatewayProxyResult } from "aws-lambda";
import createHttpError from "http-errors";
// This is your AWS handler
async function helloWorld(
event: APIGatewayEvent
): Promise<APIGatewayProxyResult> {
if (event.queryStringParameters?.search == null) {
// If you throw an error with status code, the error will be returned as stringified JSON
// Only the stack will be omitted.
throw createHttpError(400, "Query has to include a search");
}
// If you throw an error with no status code, only a generic message will be shown to the user
// instead of the full error
throw new Error("Search is not implemented yet");
}
// Wrap the handler with the middleware
export const handler = errorHandler()(helloWorld);
```
[![npm version](https: