sails-hook-exception-handling
v0.1.1
Published
Forget about the try/catch in the middlewares
Downloads
2
Maintainers
Readme
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: '[email protected]' }, { username: '[email protected]' }) // throws {code: E_UNIQUE} error
await User.findOne({ invalidAttr: '[email protected]' }) // 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