Skip to content

Commit 437e10c

Browse files
committed
Merge context with set{Extra,Tags}Context instead of clobbering
Fixes #255
1 parent ecdf939 commit 437e10c

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* DEPRECATED: Replace `Raven.setReleaseContext` with `Raven.setRelease`.
99
* NEW: Add `Raven.clearContext()` to empty all of the context.
1010
* NEW: Add `Raven.getContext()` to get a copy of the current context.
11+
* NEW: `Raven.set{Extra,Tags}Context(ctx)` now merges with existing values instead of overwriting.
1112

1213
## 1.1.22
1314

src/raven.js

+13-4
Original file line numberDiff line numberDiff line change
@@ -295,31 +295,40 @@ var Raven = {
295295
* @return {Raven}
296296
*/
297297
setUserContext: function(user) {
298+
// Intentionally do not merge here since that's an unexpected behavior.
298299
globalContext.user = user;
299300

300301
return Raven;
301302
},
302303

303304
/*
304-
* Set extra attributes to be sent along with the payload.
305+
* Merge extra attributes to be sent along with the payload.
305306
*
306307
* @param {object} extra An object representing extra data [optional]
307308
* @return {Raven}
308309
*/
309310
setExtraContext: function(extra) {
310-
globalContext.extra = extra || {};
311+
if (isUndefined(extra)) {
312+
delete globalContext.extra;
313+
} else {
314+
globalContext.extra = objectMerge(globalContext.extra || {}, extra);
315+
}
311316

312317
return Raven;
313318
},
314319

315320
/*
316-
* Set tags to be sent along with the payload.
321+
* Merge tags to be sent along with the payload.
317322
*
318323
* @param {object} tags An object representing tags [optional]
319324
* @return {Raven}
320325
*/
321326
setTagsContext: function(tags) {
322-
globalContext.tags = tags || {};
327+
if (isUndefined(tags)) {
328+
delete globalContext.tags;
329+
} else {
330+
globalContext.tags = objectMerge(globalContext.tags || {}, tags);
331+
}
323332

324333
return Raven;
325334
},

test/raven.test.js

+14-2
Original file line numberDiff line numberDiff line change
@@ -1843,7 +1843,13 @@ describe('Raven (public API)', function() {
18431843
it('should clear globalContext.extra with no arguments', function() {
18441844
globalOptions.extra = {name: 'Matt'};
18451845
Raven.setExtraContext();
1846-
assert.deepEqual(globalContext.extra, {});
1846+
assert.isUndefined(globalContext.extra);
1847+
});
1848+
1849+
it('should merge globalContext.extra with subsequent calls', function() {
1850+
Raven.setExtraContext({a: 1});
1851+
Raven.setExtraContext({b: 2});
1852+
assert.deepEqual(globalContext.extra, {a: 1, b: 2});
18471853
});
18481854
});
18491855

@@ -1856,7 +1862,13 @@ describe('Raven (public API)', function() {
18561862
it('should clear globalContext.tags with no arguments', function() {
18571863
globalContext.tags = {name: 'Matt'};
18581864
Raven.setTagsContext();
1859-
assert.deepEqual(globalContext.tags, {});
1865+
assert.isUndefined(globalContext.tags);
1866+
});
1867+
1868+
it('should merge globalContext.tags with subsequent calls', function() {
1869+
Raven.setTagsContext({a: 1});
1870+
Raven.setTagsContext({b: 2});
1871+
assert.deepEqual(globalContext.tags, {a: 1, b: 2});
18601872
});
18611873
});
18621874

0 commit comments

Comments
 (0)