OptimalBits / node_acl

Access control lists for node applications
2.62k stars 371 forks source link

acl/middleware: add default errorHandler #148

Closed nemtsov closed 9 years ago

nemtsov commented 9 years ago

This is a default error handler that can be used in conjunction with acl.middleware(). It is a convenience method that allows to return the proper http status code and message, instead of the default 500 error.

Basic usage:

app.get('/', acl.middleware(), function (req, res) { res.end('ok'); });
app.use(acl.middleware.errorHandler());

And a curl to this service will yield a clean http error with the correct status code and informative message:

$ curl -i http://localhost:3000/
HTTP/1.1 403 Forbidden
X-Powered-By: Express
Date: Sun, 23 Aug 2015 21:09:54 GMT
Connection: keep-alive
Transfer-Encoding: chunked

Insufficient permissions to access resource

Also supports html and json content type:

app.get('/', acl.middleware(), function (req, res) { res.end('ok'); });
app.use(acl.middleware.errorHandler('json'));
manast commented 9 years ago

Looks great, thanks!.