Skip to content

Commit ee796c3

Browse files
authored
add default payload keys before the custom transform runs (#1078)
1 parent 0727a54 commit ee796c3

File tree

5 files changed

+40
-6
lines changed

5 files changed

+40
-6
lines changed

src/browser/core.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,7 @@ function addTransformsToNotifier(notifier, rollbar, gWindow) {
502502
.addTransform(sharedTransforms.addTelemetryData)
503503
.addTransform(sharedTransforms.addConfigToPayload)
504504
.addTransform(transforms.addScrubber(rollbar.scrub))
505+
.addTransform(sharedTransforms.addPayloadOptions)
505506
.addTransform(sharedTransforms.userTransform(logger))
506507
.addTransform(sharedTransforms.addConfiguredOptions)
507508
.addTransform(sharedTransforms.addDiagnosticKeys)

src/react-native/rollbar.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ function addTransformsToNotifier(notifier) {
286286
.addTransform(sharedTransforms.addTelemetryData)
287287
.addTransform(sharedTransforms.addConfigToPayload)
288288
.addTransform(transforms.scrubPayload)
289+
.addTransform(sharedTransforms.addPayloadOptions)
289290
.addTransform(sharedTransforms.userTransform(logger))
290291
.addTransform(sharedTransforms.addConfiguredOptions)
291292
.addTransform(sharedTransforms.addDiagnosticKeys)

src/server/rollbar.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,7 @@ function addTransformsToNotifier(notifier) {
537537
.addTransform(transforms.addLambdaData)
538538
.addTransform(sharedTransforms.addConfigToPayload)
539539
.addTransform(transforms.scrubPayload)
540+
.addTransform(sharedTransforms.addPayloadOptions)
540541
.addTransform(sharedTransforms.userTransform(logger))
541542
.addTransform(sharedTransforms.addConfiguredOptions)
542543
.addTransform(sharedTransforms.addDiagnosticKeys)

src/transforms.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
var _ = require('./utility');
22

33
function itemToPayload(item, options, callback) {
4-
var payloadOptions = options.payload || {};
5-
if (payloadOptions.body) {
6-
delete payloadOptions.body;
7-
}
4+
var data = item.data;
85

9-
var data = _.merge(item.data, payloadOptions);
106
if (item._isUncaught) {
117
data._isUncaught = true;
128
}
@@ -16,6 +12,16 @@ function itemToPayload(item, options, callback) {
1612
callback(null, data);
1713
}
1814

15+
function addPayloadOptions(item, options, callback) {
16+
var payloadOptions = options.payload || {};
17+
if (payloadOptions.body) {
18+
delete payloadOptions.body;
19+
}
20+
21+
item.data = _.merge(item.data, payloadOptions);
22+
callback(null, item);
23+
}
24+
1925
function addTelemetryData(item, options, callback) {
2026
if (item.telemetryEvents) {
2127
_.set(item, 'data.body.telemetry', item.telemetryEvents);
@@ -139,6 +145,7 @@ function addDiagnosticKeys(item, options, callback) {
139145

140146
module.exports = {
141147
itemToPayload: itemToPayload,
148+
addPayloadOptions: addPayloadOptions,
142149
addTelemetryData: addTelemetryData,
143150
addMessageWithError: addMessageWithError,
144151
userTransform: userTransform,

test/transforms.test.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,20 @@ describe('itemToPayload', function() {
2121
var args = ['a message', {custom: 'stuff'}];
2222
var item = itemFromArgs(args);
2323
item.accessToken = 'abc123';
24+
item._isUncaught = true;
25+
item._originalArgs = ['c', 3];
26+
item.data = {};
2427
var options = {
2528
endpoint: 'api.rollbar.com',
2629
payload: {body: 'hey', x: 42}
2730
};
2831
t.itemToPayload(item, options, function(e, i) {
32+
expect(i._isUncaught).to.eql(item._isUncaught);
33+
expect(i._originalArgs).to.eql(item._originalArgs);
34+
35+
// This transform shouldn't apply any payload keys.
2936
expect(i.body).to.not.eql('hey');
30-
expect(i.x).to.eql(42);
37+
expect(i.x).to.not.eql(42);
3138
done(e);
3239
});
3340
});
@@ -46,6 +53,23 @@ describe('itemToPayload', function() {
4653
});
4754
});
4855

56+
describe('addPayloadOptions', function() {
57+
it('ignores options.payload.body but merges in other payload options', function(done) {
58+
var args = ['a message', {custom: 'stuff'}];
59+
var item = itemFromArgs(args);
60+
item.accessToken = 'abc123';
61+
var options = {
62+
endpoint: 'api.rollbar.com',
63+
payload: {body: 'hey', x: 42}
64+
};
65+
t.addPayloadOptions(item, options, function(e, i) {
66+
expect(i.data.body).to.not.eql('hey');
67+
expect(i.data.x).to.eql(42);
68+
done(e);
69+
});
70+
});
71+
});
72+
4973
describe('addTelemetryData', function() {
5074
it('adds the data to the right place if events exist', function(done) {
5175
var item = {

0 commit comments

Comments
 (0)