Server side error handling service, best suited for promise chains, uses 'http-errors'
npm install http-errors-promisehttp-errors.js
const error = require('http-errors-promise');
router.get('/', function(req, res, next) {
getStuffFromDB()
.then(//...)
.catch(function (error) {
return error.respond(
res,
error, // error from the call 'getStuffFromDB'
'Something went wrong' // error from this context
);
});
});
`
Shining Use-case
`js
function doSomething() {
// returns a promise
return new Promise((resolve, reject) => {
// doing some stuff...
if (someCondition) {
resolve(result);
} else {
return error(null, 'messed up while doing something', 500);
}
})
}function doSomethingElse() {
// calls 'doSomething', does its own processing and returns a promise
return doSomething()
.then(result => {
// does some stuff
return newResult;
})
.catch(err => error(
err,
'Messed up while doing somethingElse'
));
}
function mainFunction() {
doSomethingElse()
.catch(err => {
// here 'err' could either be from 'doSomething' or
// 'doSomethingElse' based on what went wrong and where
// the error actually originated from, more technically the
// error properly bubbles up
})
}
`Methods
$3
- err error object recieved from a function call
- secErr error from the current context
- status http status, defaults to 500
- returns a rejected promise with the resulting error
- dontReject returns the resulting error instead of a rejected promise
$3
- similar to error method, except that it takes the express' response object
and sends the response with the error instead of returning$3
- creates and returns an error using http-errors$3
- creates and returns commonly seen errors by using a dynamically set modelName in the error message, also using http-errors
- the common errors are defined in common.js` (you're welcome to add new errors there)
Design inspired by ralusek's work on error handling