Skip to content

Commit

Permalink
Merge pull request OptimalBits#148 from nemtsov/patch-4
Browse files Browse the repository at this point in the history
acl/middleware: add default errorHandler
  • Loading branch information
manast committed Aug 23, 2015
2 parents ec4f24a + 9132f6a commit 9bc0a24
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions lib/acl.js
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,27 @@ Acl.prototype.middleware = function(numPathComponents, userId, actions){
};
};

/**
Error handler for the Express middleware
@param {String} [contentType] (html|json) defaults to plain text
*/
Acl.prototype.middleware.errorHandler = function(contentType){
var method = 'end';

if(contentType){
switch (contentType) {
case 'json': method = 'json'; break;
case 'html': method = 'send'; break;
}
}

return function(err, req, res, next){
if(err.name !== 'HttpError' || !err.errorCode) return next(err);
res.status(err.errorCode)[method](err.message);
};
};


//-----------------------------------------------------------------------------
//
Expand Down

0 comments on commit 9bc0a24

Please sign in to comment.