From 3144142d266b41edb052a460b1fbc4f744b0cf2d Mon Sep 17 00:00:00 2001 From: Douwe Maan Date: Wed, 25 Jun 2014 16:47:26 +0200 Subject: [PATCH] Add Raven.setExtraContext and Raven.setTagsContext. --- docs/usage/index.rst | 4 ++-- example/index.html | 2 +- src/raven.js | 28 +++++++++++++++++++++++++++- test/raven.test.js | 32 +++++++++++++++++++++++++++++--- 4 files changed, 59 insertions(+), 7 deletions(-) diff --git a/docs/usage/index.rst b/docs/usage/index.rst index 376329dce1e3..3381ab168bfb 100644 --- a/docs/usage/index.rst +++ b/docs/usage/index.rst @@ -58,12 +58,12 @@ While a user is logged in, you can tell Sentry to associate errors with user dat .. code-block:: javascript - Raven.setUser({ + Raven.setUserContext({ email: 'matt@example.com', id: '123' }) -If at any point, the user becomes unauthenticated, you can call ``Raven.setUser()`` with no arguments to remove their data. *This would only really be useful in a large web app where the user logs in/out without a page reload.* +If at any point, the user becomes unauthenticated, you can call ``Raven.setUserContext()`` with no arguments to remove their data. *This would only really be useful in a large web app where the user logs in/out without a page reload.* Capturing a specific message ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/example/index.html b/example/index.html index abc350999a4d..bf9bd100e34c 100644 --- a/example/index.html +++ b/example/index.html @@ -14,7 +14,7 @@ //awesome Raven.config('http://50dbe04cd1224d439e9c49bf1d0464df@localhost:8000/1').install(); -Raven.setUser({ +Raven.setUserContext({ email: 'matt@ydekproductions.com', id: 5 }) diff --git a/src/raven.js b/src/raven.js index 3ed31f7d5b23..be0743170561 100644 --- a/src/raven.js +++ b/src/raven.js @@ -264,12 +264,36 @@ var Raven = { * @param {object} user An object representing user data [optional] * @return {Raven} */ - setUser: function(user) { + setUserContext: function(user) { globalUser = user; return Raven; }, + /* + * Set extra attributes to be sent along with the payload. + * + * @param {object} extra An object representing extra data [optional] + * @return {Raven} + */ + setExtraContext: function(extra) { + globalOptions.extra = extra || {}; + + return Raven; + }, + + /* + * Set tags to be sent along with the payload. + * + * @param {object} tags An object representing tags [optional] + * @return {Raven} + */ + setTagsContext: function(tags) { + globalOptions.tags = tags || {}; + + return Raven; + }, + /* * Get the latest raw exception that was captured by Raven. * @@ -289,6 +313,8 @@ var Raven = { } }; +Raven.setUser = Raven.setUserContext; // To be deprecated + function triggerEvent(eventType, options) { var event, key; diff --git a/test/raven.test.js b/test/raven.test.js index 4c6018cf8666..35619628158c 100644 --- a/test/raven.test.js +++ b/test/raven.test.js @@ -1361,19 +1361,45 @@ describe('Raven (public API)', function() { }); }); - describe('.setUser', function() { + describe('.setUserContext', function() { it('should set the globalUser object', function() { - Raven.setUser({name: 'Matt'}); + Raven.setUserContext({name: 'Matt'}); assert.deepEqual(globalUser, {name: 'Matt'}); }); it('should clear the globalUser with no arguments', function() { globalUser = {name: 'Matt'}; - Raven.setUser(); + Raven.setUserContext(); assert.isUndefined(globalUser); }); }); + describe('.setExtraContext', function() { + it('should set the globalOptions.extra object', function() { + Raven.setExtraContext({name: 'Matt'}); + assert.deepEqual(globalOptions.extra, {name: 'Matt'}); + }); + + it('should clear globalOptions.extra with no arguments', function() { + globalOptions = {name: 'Matt'}; + Raven.setExtraContext(); + assert.deepEqual(globalOptions.extra, {}); + }); + }); + + describe('.setTagsContext', function() { + it('should set the globalOptions.tags object', function() { + Raven.setTagsContext({name: 'Matt'}); + assert.deepEqual(globalOptions.tags, {name: 'Matt'}); + }); + + it('should clear globalOptions.tags with no arguments', function() { + globalOptions = {name: 'Matt'}; + Raven.setTagsContext(); + assert.deepEqual(globalOptions.tags, {}); + }); + }); + describe('.captureMessage', function() { it('should work as advertised', function() { this.sinon.stub(window, 'send');