From 557185b5c7ef5b6085cefcf300973adac43d9cb4 Mon Sep 17 00:00:00 2001 From: Lewis Ellis Date: Tue, 7 Feb 2017 17:47:41 -0800 Subject: [PATCH 1/3] Bind req/res to context domain in express requestHandler --- lib/client.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/client.js b/lib/client.js index ba1f854..57c7b42 100644 --- a/lib/client.js +++ b/lib/client.js @@ -392,7 +392,11 @@ extend(Raven.prototype, { requestHandler: function () { var self = this; return function (req, res, next) { - self.context({}, next, next); + self.context({}, function () { + domain.active.add(req); + domain.active.add(res); + next(); + }, next); }; }, From aef083300fe4962d9ff34822d1bb96154d6c5f0e Mon Sep 17 00:00:00 2001 From: Lewis Ellis Date: Tue, 7 Feb 2017 18:33:05 -0800 Subject: [PATCH 2/3] Add test for explicit adding req/res to context domain in middleware --- test/raven.client.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/test/raven.client.js b/test/raven.client.js index 6b0ac1c..85f5196 100644 --- a/test/raven.client.js +++ b/test/raven.client.js @@ -1018,4 +1018,29 @@ describe('raven.middleware', function () { client2.should.not.equal(client1); client2.should.be.an.instanceof(raven.constructor); }); + + it('should explicitly add req and res to the domain', function (done) { + var client = new raven.Client(dsn).install(); + + var EventEmitter = require('events'); + var e = new EventEmitter(); + e.on('done', function () { + // Context won't propagate here without the explicit binding of req/res done in the middleware + setTimeout(function () { + client.getContext().breadcrumbs.length.should.equal(1); + done(); + }, 0); + }); + + // Pass e as the req/res, so e will be added to the domain + client.requestHandler()(e, e, function () { + client.captureBreadcrumb({ + message: 'test breadcrumb', + category: 'log' + }); + setTimeout(function () { + e.emit('done'); + }, 0); + }); + }); }); From 146c474f4fc17751d0eb44cdec7f300236c98a45 Mon Sep 17 00:00:00 2001 From: Lewis Ellis Date: Tue, 7 Feb 2017 18:46:11 -0800 Subject: [PATCH 3/3] Make explicit binding test work on Node 0.10 --- test/raven.client.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/test/raven.client.js b/test/raven.client.js index 85f5196..ea1e890 100644 --- a/test/raven.client.js +++ b/test/raven.client.js @@ -1021,13 +1021,16 @@ describe('raven.middleware', function () { it('should explicitly add req and res to the domain', function (done) { var client = new raven.Client(dsn).install(); + var message = 'test breadcrumb'; var EventEmitter = require('events'); + if (process.version <= 'v0.11') EventEmitter = EventEmitter.EventEmitter; // node 0.10 compat var e = new EventEmitter(); e.on('done', function () { // Context won't propagate here without the explicit binding of req/res done in the middleware setTimeout(function () { client.getContext().breadcrumbs.length.should.equal(1); + client.getContext().breadcrumbs[0].message.should.equal(message); done(); }, 0); }); @@ -1035,7 +1038,7 @@ describe('raven.middleware', function () { // Pass e as the req/res, so e will be added to the domain client.requestHandler()(e, e, function () { client.captureBreadcrumb({ - message: 'test breadcrumb', + message: message, category: 'log' }); setTimeout(function () {