Express middleware that sends errors in JSON-API compliant format.
It prevents error.stack leaking in non-development mode.
ES6 compatible node@^4.3.1
engine required.
const app = require('express')()
const jsonapiErrors = require('jsonapi-errors')
// import errors
const errors = require('jsonapi-errors/lib/errors')
const BadRequestError = errors.BadRequestError
const DbError = errors.DbError
const ForbiddenError = errors.ForbiddenError
const NotFoundError = errors.NotFoundError
const UnauthorizedError = errors.UnauthorizedError
// or with 'node --harmony_destructuring' flag
const {
BadRequestError,
DbError,
ForbiddenError,
NotFoundError,
UnauthorizedError
} = require('jsonapi-errors/lib/errors')
// place here json-api routes
// they handle errors this way: next(new ForbiddenError('User has insufficient access rights'))
// place jsonapi-errors middleware after all api routes
// so it can catch, format, and send errors in JSON-API format
app.use(jsonapiErrors)
The json result of
next(new ForbiddenError('User has insufficient access rights'))
will be
{
"errors": [
{
"status": "403",
"title": "Forbidden",
"detail": "User has insufficient access rights"
}
]
}
It is licensed under the MIT License.