Skip to content

Commit 5ad3ad2

Browse files
authored
Simplify context methods to fall back on instance-global context (#226)
* Simplify context methods to fall back on instance-global context * Fix setContext, add merging hierarchy w/tests * Simplify errorHandler middleware, call next immediately instead of waiting for capture * Deprecate setUser/Tags/ExtraContext * Rename/fix updateContext -> mergeContext
1 parent dbec003 commit 5ad3ad2

File tree

2 files changed

+67
-79
lines changed

2 files changed

+67
-79
lines changed

lib/client.js

Lines changed: 22 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ extend(Raven.prototype, {
9393
eventId = this.generateEventId();
9494
}
9595

96+
var domainContext = domain.active && domain.active.sentryContext || {};
97+
kwargs.user = extend({}, this._globalContext.user, domainContext.user, kwargs.user);
98+
kwargs.tags = extend({}, this._globalContext.tags, domainContext.tags, kwargs.tags);
99+
kwargs.extra = extend({}, this._globalContext.extra, domainContext.extra, kwargs.extra);
100+
96101
kwargs.modules = utils.getModules();
97102
kwargs.server_name = kwargs.server_name || this.name;
98103

@@ -101,19 +106,12 @@ extend(Raven.prototype, {
101106
}
102107

103108
kwargs.environment = kwargs.environment || this.environment;
104-
kwargs.extra = extend({}, this._globalContext.extra, kwargs.extra);
105-
kwargs.tags = extend({}, this._globalContext.tags, kwargs.tags);
106-
107109
kwargs.logger = kwargs.logger || this.loggerName;
108110
kwargs.event_id = eventId;
109111
kwargs.timestamp = new Date().toISOString().split('.')[0];
110112
kwargs.project = this.dsn.project_id;
111113
kwargs.platform = 'node';
112114

113-
if (this._globalContext.user) {
114-
kwargs.user = this._globalContext.user || kwargs.user;
115-
}
116-
117115
// Only include release information if it is set
118116
if (this.release) {
119117
kwargs.release = this.release;
@@ -287,28 +285,21 @@ extend(Raven.prototype, {
287285
},
288286

289287
setContext: function setContext(ctx) {
290-
if (!domain.active) {
291-
utils.consoleAlert('attempt to setContext outside context scope');
292-
} else {
288+
if (domain.active) {
293289
domain.active.sentryContext = ctx;
290+
} else {
291+
this._globalContext = ctx;
294292
}
293+
return this;
295294
},
296295

297-
// todo consider this naming; maybe "mergeContext" instead?
298-
updateContext: function updateContext(ctx) {
299-
if (!domain.active) {
300-
utils.consoleAlert('attempt to updateContext outside context scope');
301-
} else {
302-
domain.active.sentryContext = extend({}, domain.active.sentryContext, ctx);
303-
}
296+
mergeContext: function mergeContext(ctx) {
297+
extend(this.getContext(), ctx);
298+
return this;
304299
},
305300

306301
getContext: function getContext() {
307-
if (!domain.active) {
308-
utils.consoleAlert('attempt to getContext outside context scope');
309-
return null;
310-
}
311-
return domain.active.sentryContext;
302+
return domain.active ? domain.active.sentryContext : this._globalContext;
312303
},
313304

314305
/*
@@ -317,9 +308,8 @@ extend(Raven.prototype, {
317308
* @param {object} user An object representing user data [optional]
318309
* @return {Raven}
319310
*/
320-
setUserContext: function setUserContext(user) {
321-
utils.consoleAlert('setUserContext has been deprecated and will be removed in v2.0');
322-
this._globalContext.user = user;
311+
setUserContext: function setUserContext() {
312+
utils.consoleAlert('setUserContext has been deprecated and will be removed in v2.0; use setContext instead');
323313
return this;
324314
},
325315

@@ -329,9 +319,8 @@ extend(Raven.prototype, {
329319
* @param {object} extra An object representing extra data [optional]
330320
* @return {Raven}
331321
*/
332-
setExtraContext: function setExtraContext(extra) {
333-
utils.consoleAlert('setExtraContext has been deprecated and will be removed in v2.0');
334-
this._globalContext.extra = extend({}, this._globalContext.extra, extra);
322+
setExtraContext: function setExtraContext() {
323+
utils.consoleAlert('setExtraContext has been deprecated and will be removed in v2.0; use setContext instead');
335324
return this;
336325
},
337326

@@ -341,9 +330,8 @@ extend(Raven.prototype, {
341330
* @param {object} tags An object representing tags [optional]
342331
* @return {Raven}
343332
*/
344-
setTagsContext: function setTagsContext(tags) {
345-
utils.consoleAlert('setTagsContext has been deprecated and will be removed in v2.0');
346-
this._globalContext.tags = extend({}, this._globalContext.tags, tags);
333+
setTagsContext: function setTagsContext() {
334+
utils.consoleAlert('setTagsContext has been deprecated and will be removed in v2.0; use setContext instead');
347335
return this;
348336
},
349337

@@ -398,13 +386,9 @@ extend(Raven.prototype, {
398386
if (status < 500) return next(err);
399387

400388
var kwargs = parsers.parseRequest(req);
401-
if (domain.active && domain.active.sentryContext) {
402-
kwargs = extend(kwargs, domain.active.sentryContext);
403-
}
404-
return self.captureException(err, kwargs, function (sendErr, eventId) {
405-
res.sentry = eventId;
406-
next(err);
407-
});
389+
var eventId = self.captureException(err, kwargs);
390+
res.sentry = eventId;
391+
return next(err);
408392
};
409393
}
410394
});

test/raven.client.js

Lines changed: 45 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -748,9 +748,11 @@ describe('raven.Client', function () {
748748
release: 'version1'
749749
});
750750

751-
client.setUserContext({
752-
753-
id: '123'
751+
client.setContext({
752+
user: {
753+
754+
id: '123'
755+
}
754756
});
755757

756758
client.on('logged', function () {
@@ -788,52 +790,54 @@ describe('raven.Client', function () {
788790
});
789791
});
790792

791-
describe('#setUserContext()', function () {
792-
it('should add the user object to the globalContext', function () {
793-
var user = {
794-
email: '[email protected]', // <-- my fave user
795-
id: '123'
796-
};
797-
798-
client.setUserContext(user);
799-
800-
client._globalContext.user.should.equal(user);
793+
describe('#setContext', function () {
794+
afterEach(function () {
795+
process.domain && process.domain.exit();
801796
});
802-
});
803797

804-
describe('#setExtraContext()', function () {
805-
it('should merge the extra data object into the globalContext', function () {
806-
// when no pre-existing context
807-
client.setExtraContext({
808-
bar: 'baz'
809-
});
810-
811-
client._globalContext.extra.should.have.property('bar', 'baz');
798+
it('should merge contexts in correct hierarchy', function (done) {
799+
var scope = nock('https://app.getsentry.com')
800+
.filteringRequestBody(/.*/, '*')
801+
.post('/api/269/store/', '*')
802+
.reply(200, function (uri, body) {
803+
zlib.inflate(new Buffer(body, 'base64'), function (err, dec) {
804+
if (err) return done(err);
805+
var msg = JSON.parse(dec.toString());
812806

813-
client.setExtraContext({ // should merge onto previous
814-
foo: 'bar'
815-
});
807+
msg.user.should.eql({
808+
a: 1,
809+
b: 2,
810+
c: 3
811+
});
816812

817-
client._globalContext.extra.should.have.property('foo', 'bar');
818-
client._globalContext.extra.should.have.property('bar', 'baz');
819-
});
820-
});
813+
done();
814+
});
815+
return 'OK';
816+
});
821817

822-
describe('#setTagsContext()', function () {
823-
it('should merge the extra data object into the globalContext', function () {
824-
// when no pre-existing context
825-
client.setTagsContext({
826-
browser: 'Chrome'
818+
client.setContext({
819+
user: {
820+
a: 1,
821+
b: 1,
822+
c: 1
823+
}
827824
});
828825

829-
client._globalContext.tags.should.have.property('browser', 'Chrome');
830-
831-
client.setTagsContext({ // should merge onto previous
832-
platform: 'OS X'
826+
client.context(function () {
827+
client.setContext({
828+
user: {
829+
b: 2,
830+
c: 2
831+
}
832+
});
833+
client.captureException(new Error('foo'), {
834+
user: {
835+
c: 3
836+
}
837+
}, function () {
838+
scope.done();
839+
});
833840
});
834-
835-
client._globalContext.tags.should.have.property('browser', 'Chrome');
836-
client._globalContext.tags.should.have.property('platform', 'OS X');
837841
});
838842
});
839843

0 commit comments

Comments
 (0)