Continue middleware chain after response or error

This commit is contained in:
Esther Lozano 2020-02-25 14:14:20 +01:00
parent 970be73052
commit 8f81c810e0
3 changed files with 35 additions and 18 deletions

View File

@ -37,6 +37,8 @@ module.exports = function errorMiddleware (/* options */) {
} else {
res.json(errorResponseBody);
}
next();
};
};

View File

@ -1,19 +1,22 @@
'use strict';
module.exports = function sendResponse () {
return function sendResponseMiddleware (req, res) {
return function sendResponseMiddleware (req, res, next) {
req.profiler.done('res');
res.status(res.statusCode);
if (Buffer.isBuffer(res.body)) {
return res.send(res.body);
res.send(res.body);
return next();
}
if (req.query.callback) {
return res.jsonp(res.body);
res.jsonp(res.body);
return next();
}
res.json(res.body);
return next();
};
};

View File

@ -62,13 +62,17 @@ describe('error-middleware', function () {
};
const errorFn = errorMiddleware();
errorFn(errors, req, res);
errorFn(errors, req, res, (err) => {
if (err) {
return done(err);
}
assert.deepStrictEqual(res.headers, {
'X-Tiler-Errors': JSON.stringify(errorHeader)
assert.deepStrictEqual(res.headers, {
'X-Tiler-Errors': JSON.stringify(errorHeader)
});
return done();
});
done();
});
it('JSONP should return a header with error status code', function (done) {
@ -114,13 +118,17 @@ describe('error-middleware', function () {
};
const errorFn = errorMiddleware();
errorFn(errors, req, res);
errorFn(errors, req, res, (err) => {
if (err) {
return done(err);
}
assert.deepStrictEqual(res.headers, {
'X-Tiler-Errors': JSON.stringify(errorHeader)
assert.deepStrictEqual(res.headers, {
'X-Tiler-Errors': JSON.stringify(errorHeader)
});
return done();
});
done();
});
it('should escape chars that broke logs regex', function (done) {
@ -167,12 +175,16 @@ describe('error-middleware', function () {
};
const errorFn = errorMiddleware();
errorFn(errors, req, res);
errorFn(errors, req, res, (err) => {
if (err) {
return done(err);
}
assert.deepStrictEqual(res.headers, {
'X-Tiler-Errors': JSON.stringify(errorHeader)
assert.deepStrictEqual(res.headers, {
'X-Tiler-Errors': JSON.stringify(errorHeader)
});
return done();
});
done();
});
});