triAGENS/ArangoDB — pull request #1022 by pluma (2014-09-15 22:40:01+00:00)
Title: Allow defining common properties shared by all routes handled by a controller

Currently there's no easy way to define error responses for errors shared by all route handlers of a controller, such as `DocumentNotFound` style errors, without specifying the `errorResponse` for each and every route.

In practice this discourages using `errorResponse` and leads to exceptions bubbling up all the way to ArangoDB's route handler which generates an ugly (but informative) stack trace plaintext response.

This proposed change would allow the following syntax:

```js
var err = require('./my/api/errors');
ctrl.common(function (routeHandler) {
routeHandler.errorResponse(err.Unauthorized, 401, 'Not authenticated.');
routeHandler.errorResponse(err.NotFound, 404, 'Document not found.');
routeHandler.errorResponse(err.ImATeapot, 418, 'I\'m a teapot.');
});

ctrl.get('/some/route', function (req, res) {
// ...
throw new err.NotFound('The document does not exist');
// ...
}); // no errorResponse needed here

ctrl.get('/another/route', function (req, res) {
// ...
throw new err.NotFound('I made you a cookie but I ated it');
// ...
}); // no errorResponse needed here either!
```