sails-hook-exception-handling

0.1.1 • Public • Published

sails-hook-exception-handling

Capture all the errors that are thrown from the middlewares without adding try/catch in all of them.

Installation

$ npm install --save sails-hook-exception-handling

Sails v1.0 >= required, responses hook must be enabled.

Usage

await User.update({ email: 'foo@foo.com' }, { username: 'alreadyTakenEmail@foo.com' }) // throws {code: E_UNIQUE} error
await User.findOne({ invalidAttr: 'foo@foo.com' }) // throws {name: UsageError} error

This hooks will manage al the Waterline errors, sending to the user the error with the pertinent status. More info about Waterline errors.

  'get /not-found': (req, res) => {
    throw 'notFound'; // sends res.notFound()
  },
  'get /bad-request': (req, res) => {
    throw 'badRequest'; // sends res.badRequest()
  },
  'get /forbidden': (req, res) => {
    throw 'forbidden'; // sends res.forbidden()
  },

All the unnexpected errors will be handled as status 500, with res.serverError(err) default method.

Test

// mocha required: npm i -g mocha
 
npm test

Author

@josebaseba

Package Sidebar

Install

npm i sails-hook-exception-handling

Weekly Downloads

1

Version

0.1.1

License

MIT

Unpacked Size

7.3 kB

Total Files

5

Last publish

Collaborators

  • josebaseba