I'm calling some database functions from my request handlers. All of these functions do separate "error callbacks" for handling errors. Example:
function referralComplete(req, res) {
/*getting id etc.*/
db.startDatabaseConnection(function() {
db.flagReferralAsDone(id, function(success) {
db.endDatabaseConnection();
/*doing stuff on success*/
}, onError);
}, onError);
function onError(err, description) {
logger.error(description + ": " + err);
user.pageNotFound(req, res);
}
}
I have several request handlers similar to this all calling different database functions. At the moment I have duplicated onError()
into the scope of each of them since I need req
and res
when handling the error, but I like think that there might be a way to achieve the same without the duplication.
So the question is, would it be possible to somehow bind req
and res
to onError()
so that I won't have to duplicate onError()
into each request handler?