@valbo/error-handler-middleware

Express error middleware that sends the error as a json response.

Usage no npm install needed!

<script type="module">
  import valboErrorHandlerMiddleware from 'https://cdn.skypack.dev/@valbo/error-handler-middleware';
</script>

README

@valbo/error-handler-middleware

Express error middleware that sends the error as a json response.

npm (scoped) semantic-release Build Status Coverage Status Known Vulnerabilities

Install

npm install @valbo/error-handler-middleware

Usage

Add it to express at the end of the middleware chain to capture the error thrown and send it as a JSON response:

import { errorHandlerMiddleware } from '@valbo/error-handler-middleware';

// at the end of the middleware chain
app.use(errorHandlerMiddleware);

This error response will have the following format:

{
  "error" : {
    "status": 400,
    "name": "BadRequestError",
    "message": ".find should NOT have additional properties"
  }
}

The status will be error.status, error.statusCode or 500 as a default.

The name will be error.name or InternalServerError as a default.

It will also store the error sent in response.locals.responseError if you want to do something else with it, like logging.