0.1.2 • Published 8 years ago

jsonapi-errors v0.1.2

Weekly downloads
2
License
MIT
Repository
github
Last release
8 years ago

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.

0.1.2

8 years ago

0.1.1

8 years ago

0.1.0

8 years ago

0.0.2

8 years ago

0.0.1

8 years ago