Skip to content

Commit ecdf939

Browse files
committed
Add {clear,get}Context methods
Fixes #286
1 parent 536dd76 commit ecdf939

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
* NEW: Add ability to swap in a custom transport. Adds `Raven.setTransport`, and `transport` option to config. Docs: https://docs.getsentry.com/hosted/clients/javascript/config/
77
* CHANGE: Always expose `Raven` to `window`. Please call `Raven.noConflict()` if you want it restored to what it was. See: https://github.com/getsentry/raven-js/pull/393
88
* DEPRECATED: Replace `Raven.setReleaseContext` with `Raven.setRelease`.
9+
* NEW: Add `Raven.clearContext()` to empty all of the context.
10+
* NEW: Add `Raven.getContext()` to get a copy of the current context.
911

1012
## 1.1.22
1113

src/raven.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,27 @@ var Raven = {
324324
return Raven;
325325
},
326326

327+
/*
328+
* Clear all of the context.
329+
*
330+
* @return {Raven}
331+
*/
332+
clearContext: function() {
333+
globalContext = {};
334+
335+
return Raven;
336+
},
337+
338+
/*
339+
* Get a copy of the current context. This cannot be mutated.
340+
*
341+
* @return {object} copy of context
342+
*/
343+
getContext: function() {
344+
// lol javascript
345+
return JSON.parse(JSON.stringify(globalContext));
346+
},
347+
327348
/*
328349
* Set release version of application
329350
*

test/raven.test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1860,6 +1860,25 @@ describe('Raven (public API)', function() {
18601860
});
18611861
});
18621862

1863+
describe('.clearContext', function() {
1864+
it('should clear the globalContext object', function() {
1865+
globalContext = {tags: {}, extra: {}, user: {}};
1866+
Raven.clearContext();
1867+
assert.deepEqual(globalContext, {});
1868+
});
1869+
});
1870+
1871+
describe('.getContext', function() {
1872+
it('should retrieve a copy of the current context', function() {
1873+
globalContext = {tags: {a: 1}};
1874+
var context = Raven.getContext();
1875+
assert.deepEqual(globalContext, context);
1876+
context.tags.a = 2;
1877+
// It shouldn't have mutated the original
1878+
assert.equal(globalContext.tags.a, 1);
1879+
});
1880+
});
1881+
18631882
describe('.setRelease', function() {
18641883
it('should set the globalOptions.release attribute', function() {
18651884
Raven.setRelease('abc123');

0 commit comments

Comments
 (0)