This package has been deprecated

Author message:

Package no longer supported. Contact Support at https://www.npmjs.com/support for more info.

express-json-api-error-handler
TypeScript icon, indicating that this package has built-in type declarations

6.0.1 • Public • Published

code style: prettier Build Status codecov

Express JSON:API Error Handler

Error handling middleware for Node/Express applications. All detected errors are finally transformed into JSON:API errors.

Installation

Installation is done using the npm install command:

$ npm i express-json-api-error-handler

Features

ErrorHandler

The library exposes the ErrorHandler which uses:

  1. The handle method as the express middleware
  2. The setErrorEventHandler method for setting a callback when an error is raised
  3. You can initialize with setting options. The available options for now are
{
  buildMetaboolean // To build the meta of the error not. Defaults to false.
}

Example on how to initialize the event handler

import { ErrorHandler } from `express-json-api-error-handler`;
 
const errorHandler = new ErrorHandler({ buildMeta: true});
errorHandler.setErrorEventHandler((err) => { console.log(err)})
 
app.use(errorHandler.handle)

Error structure

The error the handler produces is of the following structure

 
{
      'errors': [
        {
          'code': '1200',
          'detail': 'Mock error description',
          'status': '403',
          'title': 'Error 403',
        },
      ],
      'jsonapi': {
        'version': '1.0',
      },
      'meta': {
        'request_id': '12345',
      },
    };
 

Errors

The library provides some errors you can use in you app. The available errors are AuthError, ForbiddenError, InternalServerError, NotFoundError.

You can use them like this

import { AuthError } from `express-json-api-error-handler`;
 
next(new AuthError('The user is not authorized!', 5555))
 

Test

npm install

npm test

All feedback, issues or suggestions are welcomed :)

Package Sidebar

Install

npm i express-json-api-error-handler

Weekly Downloads

47

Version

6.0.1

License

ISC

Unpacked Size

89.3 kB

Total Files

142

Last publish

Collaborators

  • kioannou