Skip to content

Commit e86c5bf

Browse files
arnaudvallesteren
authored andcommitted
Allow more details context config in reports. (#18)
* Allow more details context config in reports. So far, the payload was only sending a limited set of context information related to the httpRequest. We can now, for example, specify a user when starting the error reporter. * Allow to set the user after the initialization It can also be set to undefined which means the user is not logged in. * Add tests for context initialization and setting user
1 parent 8aa90b8 commit e86c5bf

2 files changed

Lines changed: 36 additions & 5 deletions

File tree

stackdriver-errors.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
/**
3131
* Initialize the StackdriverErrorReporter object.
3232
* @param {Object} config - the init configuration.
33+
* @param {Object} [config.context={}] - the context in which the error occurred.
34+
* @param {string} [config.context.user] - the user who caused or was affected by the error.
3335
* @param {String} config.key - the API key to use to call the API.
3436
* @param {String} config.projectId - the Google Cloud Platform project ID to report errors to.
3537
* @param {String} [config.service=web] - service identifier.
@@ -47,6 +49,7 @@
4749

4850
this.apiKey = config.key;
4951
this.projectId = config.projectId;
52+
this.context = config.context || {};
5053
this.serviceContext = {service: config.service || 'web'};
5154
if(config.version) {
5255
this.serviceContext.version = config.version;
@@ -84,11 +87,10 @@
8487

8588
var payload = {};
8689
payload.serviceContext = this.serviceContext;
87-
payload.context = {
88-
httpRequest: {
89-
userAgent: window.navigator.userAgent,
90-
url: window.location.href
91-
}
90+
payload.context = this.context;
91+
payload.context.httpRequest = {
92+
userAgent: window.navigator.userAgent,
93+
url: window.location.href
9294
};
9395

9496
var firstFrameIndex = 0;
@@ -138,4 +140,13 @@
138140
};
139141
xhr.send(JSON.stringify(payload));
140142
};
143+
144+
/**
145+
* Set the user for the current context.
146+
*
147+
* @param {string} user - the unique identifier of the user (can be ID, email or custom token) or `undefined` if not logged in.
148+
*/
149+
StackdriverErrorReporter.prototype.setUser = function(user) {
150+
this.context.user = user;
151+
};
141152
})(this);

test/test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,16 @@ describe('Initialization', function () {
6868
expect(function() {errorHandler.start({key:'key'});}).to.throw(Error, /project/);
6969
});
7070

71+
it('should have default context', function () {
72+
errorHandler.start({key:'key', projectId:'projectId'});
73+
expect(errorHandler.context).to.eql({});
74+
});
75+
76+
it('should allow to specify a default context', function () {
77+
errorHandler.start({context: { user: '1234567890' }, key:'key', projectId:'projectId'});
78+
expect(errorHandler.context).to.eql({ user: '1234567890' });
79+
});
80+
7181
});
7282

7383
describe('Disabling', function () {
@@ -147,6 +157,16 @@ describe('Unhandled exceptions', function () {
147157

148158
});
149159

160+
describe('Setting user', function() {
161+
it('should set the user in the context', function () {
162+
errorHandler.start({key:'key', projectId:'projectId'});
163+
errorHandler.setUser('1234567890');
164+
expect(errorHandler.context.user).to.equal('1234567890');
165+
errorHandler.setUser();
166+
expect(errorHandler.context.user).to.equal(undefined);
167+
});
168+
});
169+
150170
afterEach(function() {
151171
xhr.restore();
152172
});

0 commit comments

Comments
 (0)