From 88f4fde038999867df7be8731e1a60377517e0ef Mon Sep 17 00:00:00 2001 From: Lewis J Ellis Date: Tue, 13 Dec 2016 16:52:39 -0800 Subject: [PATCH] Fix middleware backwards compatibility (#246) * Fix middleware `client instanceof Raven.Client` check to use `Raven.constructor` (thanks @dhritzkiv) * Add deprecation warning to top-level middleware * Add tests for top-level Raven.middleware backwards compat --- lib/middleware/connect.js | 21 +++++++++++++++------ test/raven.client.js | 15 +++++++++++++++ 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/lib/middleware/connect.js b/lib/middleware/connect.js index 2b0ccba..727c0e4 100644 --- a/lib/middleware/connect.js +++ b/lib/middleware/connect.js @@ -1,24 +1,33 @@ 'use strict'; var Raven = require('../client'); +var utils = require('../utils'); // Legacy support var connectMiddleware = function (client) { return connectMiddleware.errorHandler(client); }; +var getClient = function (clientOrDSN) { + // Raven is an instance, so use Raven.constructor for instanceof check + return clientOrDSN instanceof Raven.constructor ? clientOrDSN : new Raven.Client(clientOrDSN); +}; + // Error handler. This should be the last item listed in middleware, but // before any other error handlers. -connectMiddleware.errorHandler = function (client) { - client = client instanceof Raven.Client ? client : new Raven.Client(client); - return client.errorHandler(); +connectMiddleware.errorHandler = function (clientOrDSN) { + utils.consoleAlert('top-level Raven.middleware.*.errorHandler has been deprecated and will be removed in v2.0; use Raven.errorHandler() instance method instead'); + return getClient(clientOrDSN).errorHandler(); }; // Ensures asynchronous exceptions are routed to the errorHandler. This // should be the **first** item listed in middleware. -connectMiddleware.requestHandler = function (client) { - client = client instanceof Raven.Client ? client : new Raven.Client(client); - return client.requestHandler(); +connectMiddleware.requestHandler = function (clientOrDSN) { + utils.consoleAlert('top-level Raven.middleware.*.requestHandler has been deprecated and will be removed in v2.0; use Raven.requestHandler() instance method instead'); + return getClient(clientOrDSN).requestHandler(); }; +// for testing purposes only; not gonna worry about a nicer test exposure scheme since this code is going away soon +connectMiddleware.getClient = getClient; + module.exports = connectMiddleware; diff --git a/test/raven.client.js b/test/raven.client.js index 88d4792..3381a17 100644 --- a/test/raven.client.js +++ b/test/raven.client.js @@ -899,3 +899,18 @@ describe('raven.Client', function () { }); }); }); + +describe('raven.middleware', function () { + it('should use an instance passed to it instead of making a new one', function () { + var client = new raven.Client(dsn); + raven.middleware.express.getClient(client).should.equal(client); + }); + + it('should make a new instance when passed a DSN string', function () { + var client1 = new raven.Client(dsn); + var client2 = raven.middleware.express.getClient(dsn); + client2.should.not.equal(raven); + client2.should.not.equal(client1); + client2.should.be.an.instanceof(raven.constructor); + }); +});