# middy-middleware-json-error-handler

> A middy JSON error handler middleware.

Latest version **4.0.0** (published 2023-07-26) · MIT license · 0 weekly downloads

## Install

```sh
npm install middy-middleware-json-error-handler
pnpm add middy-middleware-json-error-handler
yarn add middy-middleware-json-error-handler
bun add middy-middleware-json-error-handler
```

## Health

**Score 40/100 (D)** — status: abandoned.

Positive: has types; no vulnerabilities; high maintenance score; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 4.0.0 |
| Published | 2023-07-26 |
| First published | 2020-01-23 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 15.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 31 |
| Author | Daniel Bartholomae |
| Maintainers | dbartholomae |
| Keywords | middy, middleware, error, http, json |

## Links

- npm: https://www.npmjs.com/package/middy-middleware-json-error-handler
- Repository: https://github.com/dbartholomae/middy-middleware-json-error-handler
- Homepage: https://github.com/dbartholomae/middy-middleware-json-error-handler#readme
- Issues: https://github.com/dbartholomae/middy-middleware-json-error-handler/issues
- npm.io page: https://npm.io/package/middy-middleware-json-error-handler

## Dependencies (3)

- [debug](https://npm.io/package/debug.md) ^4.3.4
- [tslib](https://npm.io/package/tslib.md) ^2.3.0
- [serialize-error](https://npm.io/package/serialize-error.md) ^8.1.0

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 4.0.0 (latest) — 2023-07-26
- 3.1.0 — 2023-07-22
- 3.0.1 — 2023-05-14
- 3.0.0 — 2021-08-01
- 2.0.6 — 2020-08-30
- 2.0.5 — 2020-08-17
- 2.0.4 — 2020-08-09
- 2.0.3 — 2020-07-15
- 2.0.2 — 2020-06-20
- 2.0.1 — 2020-05-19
- 2.0.0 — 2020-05-02
- 1.0.3 — 2020-05-01
- 1.0.2 — 2020-04-17
- 1.0.1 — 2020-04-10
- 1.0.0 — 2020-01-23

## README

# middy-middleware-json-error-handler

[![npm version](https://badge.fury.io/js/middy-middleware-json-error-handler.svg)](https://npmjs.org/package/middy-middleware-json-error-handler)
[![downloads](https://img.shields.io/npm/dw/middy-middleware-json-error-handler.svg)](https://npmjs.org/package/middy-middleware-json-error-handler)
[![open issues](https://img.shields.io/github/issues-raw/dbartholomae/middy-middleware-json-error-handler.svg)](https://github.com/dbartholomae/middy-middleware-json-error-handler/issues)
[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fdbartholomae%2Fmiddy-middleware-json-error-handler.svg?type=shield)](https://app.fossa.io/projects/git%2Bgithub.com%2Fdbartholomae%2Fmiddy-middleware-json-error-handler?ref=badge_shield)
[![debug](https://img.shields.io/badge/debug-blue.svg)](https://github.com/visionmedia/debug#readme)
[![build status](https://github.com/dbartholomae/middy-middleware-json-error-handler/workflows/Build%20and%20deploy/badge.svg?branch=main)](https://github.com/dbartholomae/middy-middleware-json-error-handler/actions?query=workflow%3A"Build+and+deploy")
[![codecov](https://codecov.io/gh/dbartholomae/middy-middleware-json-error-handler/branch/main/graph/badge.svg)](https://codecov.io/gh/dbartholomae/middy-middleware-json-error-handler)
[![semantic release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release#badge)

A [middy](https://github.com/middyjs/middy) middleware that returns errors as http errors, compatible with [http-errors](https://www.npmjs.com/package/http-errors).

## Installation
Download node at [nodejs.org](http://nodejs.org) and install it, if you haven't already.

```sh
npm install middy-middleware-json-error-handler --save
```

## Documentation

There is [additional documentation](https://dbartholomae.github.com/middy-middleware-json-error-handler).

## Usage

```typescript
import middy from '@middy/core'
import JSONErrorHandlerMiddleware from 'middy-middleware-json-error-handler'
import createHttpError from 'http-errors'
import { APIGatewayEvent } from 'aws-lambda'

// This is your AWS handler
async function helloWorld (event: APIGatewayEvent) {
  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 (event.queryStringParameters?.search === 'error') {
    // If you throw an error with status code greater than 500 and specify in options that the error
    // must be exposed, then the error will be returned as stringified JSON
    throw createHttpError(500, 'Something went wrong', { expose: true })
  }

  // 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')
}

// Specify which are the error properties to omit in response (by default, only the stack is omitted)
const options = {
  errorPropertiesToOmit: ['name', 'statusCode', 'stack']
}

// Let's "middyfy" our handler, then we will be able to attach middlewares to it (options parameter is optional)
export const handler = middy(helloWorld)
  .use(JSONErrorHandlerMiddleware(options)) // This middleware is needed do handle the errors thrown by the handler
```

---
_Source: https://npm.io/package/middy-middleware-json-error-handler · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
