Skip to content

Commit bd3bd9d

Browse files
authored
ref: Delete empty properties before sending event to the server (#1179)
* feat: Introduce isPlainObject and use it to guard isEmptyObject * ref: Delete empty properties before sending event to the server
1 parent 851129a commit bd3bd9d

File tree

4 files changed

+40
-6
lines changed

4 files changed

+40
-6
lines changed

src/raven.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1754,9 +1754,6 @@ Raven.prototype = {
17541754
};
17551755
}
17561756

1757-
// If there are no tags/extra, strip the key from the payload alltogther.
1758-
if (isEmptyObject(data.tags)) delete data.tags;
1759-
17601757
if (this._globalContext.user) {
17611758
// sentry.interfaces.User
17621759
data.user = this._globalContext.user;
@@ -1771,6 +1768,13 @@ Raven.prototype = {
17711768
// Include server_name if it's defined in globalOptions
17721769
if (globalOptions.serverName) data.server_name = globalOptions.serverName;
17731770

1771+
// Cleanup empty properties before sending them to the server
1772+
Object.keys(data).forEach(function(key) {
1773+
if (data[key] == null || data[key] === '' || isEmptyObject(data[key])) {
1774+
delete data[key];
1775+
}
1776+
});
1777+
17741778
if (isFunction(globalOptions.dataCallback)) {
17751779
data = globalOptions.dataCallback(data) || data;
17761780
}

src/utils.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ function isFunction(what) {
3434
return typeof what === 'function';
3535
}
3636

37+
function isPlainObject(what) {
38+
return Object.prototype.toString.call(what) === '[object Object]';
39+
}
40+
3741
function isString(what) {
3842
return Object.prototype.toString.call(what) === '[object String]';
3943
}
@@ -43,6 +47,8 @@ function isArray(what) {
4347
}
4448

4549
function isEmptyObject(what) {
50+
if (!isPlainObject(what)) return false;
51+
4652
for (var _ in what) {
4753
if (what.hasOwnProperty(_)) {
4854
return false;
@@ -397,6 +403,7 @@ module.exports = {
397403
isErrorEvent: isErrorEvent,
398404
isUndefined: isUndefined,
399405
isFunction: isFunction,
406+
isPlainObject: isPlainObject,
400407
isString: isString,
401408
isArray: isArray,
402409
isEmptyObject: isEmptyObject,

test/raven.test.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,7 +1136,7 @@ describe('globals', function() {
11361136
assert.isTrue(Raven._makeRequest.called);
11371137
});
11381138

1139-
it('should strip empty tags', function() {
1139+
it('should strip empty attributes', function() {
11401140
this.sinon.stub(Raven, 'isSetup').returns(true);
11411141
this.sinon.stub(Raven, '_makeRequest');
11421142
this.sinon.stub(Raven, '_getHttpData').returns({
@@ -1148,10 +1148,18 @@ describe('globals', function() {
11481148
projectId: 2,
11491149
logger: 'javascript',
11501150
maxMessageLength: 100,
1151-
tags: {}
1151+
tags: {},
1152+
extra: {}
11521153
};
11531154

1154-
Raven._send({message: 'bar', tags: {}, extra: {}});
1155+
Raven._send({
1156+
message: 'bar',
1157+
tags: {},
1158+
extra: {},
1159+
redundant: '',
1160+
attribute: null,
1161+
something: undefined
1162+
});
11551163
assert.deepEqual(Raven._makeRequest.lastCall.args[0].data, {
11561164
project: '2',
11571165
logger: 'javascript',

test/utils.test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ var RavenConfigError = require('../src/configError');
77
var utils = require('../src/utils');
88
var isUndefined = utils.isUndefined;
99
var isFunction = utils.isFunction;
10+
var isPlainObject = utils.isPlainObject;
1011
var isString = utils.isString;
1112
var isArray = utils.isArray;
1213
var isObject = utils.isObject;
@@ -42,6 +43,20 @@ describe('utils', function() {
4243
});
4344
});
4445

46+
describe('isPlainObject', function() {
47+
it('should do as advertised', function() {
48+
assert.isTrue(isPlainObject({}));
49+
assert.isTrue(isPlainObject({foo: 'bar'}));
50+
assert.isTrue(isPlainObject(new Object()));
51+
assert.isFalse(isPlainObject([]));
52+
assert.isFalse(isPlainObject(undefined));
53+
assert.isFalse(isPlainObject(null));
54+
assert.isFalse(isPlainObject(1));
55+
assert.isFalse(isPlainObject(''));
56+
assert.isFalse(isPlainObject(function() {}));
57+
});
58+
});
59+
4560
describe('isString', function() {
4661
it('should do as advertised', function() {
4762
assert.isTrue(isString(''));

0 commit comments

Comments
 (0)