jsonapi-errors

0.1.2 • Public • Published

jsonapi-errors

Express middleware that sends errors in JSON-API compliant format.

jsonapi error-objects

It prevents error.stack leaking in non-development mode.

ES6 compatible node@^4.3.1 engine required.

Usage

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"
    }
  ]
}

License

It is licensed under the MIT License.

Package Sidebar

Install

npm i jsonapi-errors

Weekly Downloads

0

Version

0.1.2

License

MIT

Last publish

Collaborators

  • alexkval