Skip to content
This repository has been archived by the owner on Oct 23, 2023. It is now read-only.

Explicitly bind req/res to context domain in express requestHandler #269

Merged
merged 3 commits into from
Feb 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
},

Expand Down
28 changes: 28 additions & 0 deletions test/raven.client.js
Original file line number Diff line number Diff line change
Expand Up @@ -1018,4 +1018,32 @@ 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 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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume that breadcrumbs[0].mesage==='test breadcrumb' here? is there a chance that some other one got added and this doesn't work?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea, good call, I added an explicit check for that.

client.getContext().breadcrumbs[0].message.should.equal(message);
done();
}, 0);
});

// Pass e as the req/res, so e will be added to the domain
client.requestHandler()(e, e, function () {
client.captureBreadcrumb({
message: message,
category: 'log'
});
setTimeout(function () {
e.emit('done');
}, 0);
});
});
});