From 461867de1d976a02499f04bd293fb4216e35ebef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20Bajto=C5=A1?= Date: Tue, 31 Jan 2017 10:02:17 +0100 Subject: [PATCH] Upgrade dependencies to their latest versions Also: - fix linting errors after upgrading eslint-config-loopback - fix a bug discovered by eslint where uknown `?_format` was throwing an unhandled error --- lib/content-negotiation.js | 6 +++--- lib/handler.js | 7 ++++++- package.json | 12 ++++++------ test/handler.test.js | 27 +++++++++++++++++---------- 4 files changed, 32 insertions(+), 20 deletions(-) diff --git a/lib/content-negotiation.js b/lib/content-negotiation.js index 7a676ed..21ff9e4 100644 --- a/lib/content-negotiation.js +++ b/lib/content-negotiation.js @@ -19,9 +19,10 @@ module.exports = negotiateContentProducer; * * @param req request object * @param {Object} options options of strong-error-handler + * @param {Function} logWarning a logger function for reporting warnings * @returns {Function} Opeartion function with signature `fn(res, data)` */ -function negotiateContentProducer(req, options) { +function negotiateContentProducer(req, logWarning, options) { var SUPPORTED_TYPES = [ 'application/json', 'json', 'text/html', 'html', @@ -66,8 +67,7 @@ function negotiateContentProducer(req, options) { // format passed through query but not supported var msg = util.format('Response _format "%s" is not supported' + 'used "%s" instead"', query._format, defaultType); - res.header('X-Warning', msg); - debug(msg); + logWarning(msg); } } diff --git a/lib/handler.js b/lib/handler.js index c37794e..8aa2004 100644 --- a/lib/handler.js +++ b/lib/handler.js @@ -51,7 +51,12 @@ exports = module.exports = function createStrongErrorHandler(options) { res.setHeader('X-Content-Type-Options', 'nosniff'); res.statusCode = data.statusCode; - var sendResponse = negotiateContentProducer(req, options); + var sendResponse = negotiateContentProducer(req, warn, options); sendResponse(res, data); + + function warn(msg) { + res.header('X-Warning', msg); + debug(msg); + } }; }; diff --git a/package.json b/package.json index cd90e28..f3c72b2 100644 --- a/package.json +++ b/package.json @@ -20,17 +20,17 @@ "accepts": "^1.3.3", "debug": "^2.2.0", "ejs": "^2.4.2", - "http-status": "^0.2.2", + "http-status": "^1.0.0", "js2xmlparser": "^2.0.2", "strong-globalize": "^2.6.7" }, "devDependencies": { - "chai": "^2.1.1", - "eslint": "^2.13.1", - "eslint-config-loopback": "^4.0.0", + "chai": "^3.5.0", + "eslint": "^3.14.1", + "eslint-config-loopback": "^8.0.0", "express": "^4.13.4", - "mocha": "^2.1.0", - "supertest": "^1.1.0" + "mocha": "^3.2.0", + "supertest": "^3.0.0" }, "browser": { "strong-error-handler": false diff --git a/test/handler.test.js b/test/handler.test.js index 1145837..86b6a81 100644 --- a/test/handler.test.js +++ b/test/handler.test.js @@ -625,6 +625,12 @@ describe('strong-error-handler', function() { .set('Accept', 'application/json') .expect('Content-Type', /^text\/html/, done); }); + + it('handles unknown _format query', function() { + givenErrorHandlerForError(); + return request.get('/?_format=unknown') + .expect('X-Warning', /_format.*not supported/); + }); }); it('does not modify "options" argument', function(done) { @@ -672,7 +678,18 @@ function setupHttpServerAndClient(done) { res.end(msg); return; } + _requestHandler(req, res, warnUnhandledError); + + function warnUnhandledError(err) { + console.log('unexpected: strong-error-handler called next with', + (err && (err.stack || err)) || 'no error'); + res.statusCode = 500; + res.setHeader('Content-Type', 'text/plain; charset=utf-8'); + res.end(err ? + 'Unhandled strong-error-handler error:\n' + (err.stack || err) : + 'The error was silently discared by strong-error-handler'); + } }); app.listen(0, function() { @@ -687,16 +704,6 @@ function setupHttpServerAndClient(done) { }); } -function warnUnhandledError(err) { - console.log('unexpected: strong-error-handler called next with ' - (err && (err.stack || err)) || 'no error'); - res.statusCode = 500; - res.setHeader('Content-Type', 'text/plain; charset=utf-8'); - res.end(err ? - 'Unhandled strong-error-handler error:\n' + (err.stack || err) : - 'The error was silently discared by strong-error-handler'); -} - function ErrorWithProps(props) { this.name = props.name || 'ErrorWithProps'; for (var p in props) {