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:
- The
handle
method as the express middleware - The
setErrorEventHandler
method for setting a callback when an error is raised - You can initialize with setting options. The available options for now are
Example on how to initialize the event handler
; ;errorHandler.setErrorEventHandler app.useerrorHandler.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
; nextnew AuthError'The user is not authorized!', 5555
Test
npm install
npm test
All feedback, issues or suggestions are welcomed :)