
var buildCustomErrorResponse = function(status) {
    return function(error, infos) {
        var response = {status: status};

        if (this.req.server.config.env === 'production') {
            error = 'An error happened. We have been notified';
        } else {
            if (typeof error === 'object') {
                error = error.message;
            }
        }

        response[error] = error;

        if (infos) {
            response.infos = infos;
        }

        this.req.logger.error(response);
        return this.status(status).json(response);
    };
};

var httpResponses = {
    notFound: buildCustomErrorResponse(404),
    forbidden: buildCustomErrorResponse(403),
    badRequest: buildCustomErrorResponse(400),
    serverError: buildCustomErrorResponse(500),
    sendResults: function(results) {
        let status = 200;
        return this.status(status).json({results: results, status: status});
    }
};

var addResponseHelpers = function(res) {
    Object.keys(httpResponses).forEach(function(responseName) {
        res[responseName] = httpResponses[responseName];
    });
};

export default function(req, res, next) {
    addResponseHelpers(res);
    return next();
}

@@ -40,20 +40,44 @@
 n(status
+, productionErrorMessage
 ) %7B%0A
-
     retu
@@ -224,16 +224,42 @@
  error =
+ productionErrorMessage %7C%7C
  'An err
@@ -441,15 +441,14 @@
 onse
-%5B
+.
 error
-%5D
  = e
@@ -689,16 +689,29 @@
 onse(404
+, 'not found'
 ),%0A    f
@@ -748,16 +748,29 @@
 onse(403
+, 'forbidden'
 ),%0A    b
@@ -808,16 +808,31 @@
 onse(400
+, 'bad request'
 ),%0A    s

add production error message