diff --git a/sdks/rollbar.js/src/browser/rollbar.js b/sdks/rollbar.js/src/browser/rollbar.js index b1ff3d46e..96e1d603a 100644 --- a/sdks/rollbar.js/src/browser/rollbar.js +++ b/sdks/rollbar.js/src/browser/rollbar.js @@ -8,6 +8,7 @@ var transport = require('./transport'); var urllib = require('./url'); var transforms = require('./transforms'); +var sharedTransforms = require('../transforms'); var predicates = require('./predicates'); var errorParser = require('./errorParser'); @@ -310,9 +311,10 @@ function addTransformsToNotifier(notifier) { .addTransform(transforms.addClientInfo(window)) .addTransform(transforms.addPluginInfo(window)) .addTransform(transforms.addBody) + .addTransform(sharedTransforms.addMessageWithError) .addTransform(transforms.scrubPayload) .addTransform(transforms.userTransform) - .addTransform(transforms.itemToPayload); + .addTransform(sharedTransforms.itemToPayload); } function addPredicatesToQueue(queue) { diff --git a/sdks/rollbar.js/src/browser/transforms.js b/sdks/rollbar.js/src/browser/transforms.js index 4c3843fb8..a0a1817f5 100644 --- a/sdks/rollbar.js/src/browser/transforms.js +++ b/sdks/rollbar.js/src/browser/transforms.js @@ -146,7 +146,7 @@ function addBodyTrace(item, options, callback) { }; if (description) { - trace.exception.description = description || 'uncaught exception'; + trace.exception.description = description; } // Transform a TraceKit stackInfo object into a Rollbar trace @@ -238,19 +238,6 @@ function userTransform(item, options, callback) { callback(null, newItem); } -function itemToPayload(item, options, callback) { - var payloadOptions = options.payload || {}; - if (payloadOptions.body) { - delete payloadOptions.body; - } - - var data = _.extend(true, {}, item.data, payloadOptions); - if (item._isUncaught) { - data._isUncaught = true; - } - callback(null, data); -} - module.exports = { handleItemWithError: handleItemWithError, ensureItemHasSomethingToSay: ensureItemHasSomethingToSay, @@ -260,6 +247,5 @@ module.exports = { addPluginInfo: addPluginInfo, addBody: addBody, scrubPayload: scrubPayload, - userTransform: userTransform, - itemToPayload: itemToPayload + userTransform: userTransform }; diff --git a/sdks/rollbar.js/src/server/rollbar.js b/sdks/rollbar.js/src/server/rollbar.js index 7dd70476b..cd6235058 100644 --- a/sdks/rollbar.js/src/server/rollbar.js +++ b/sdks/rollbar.js/src/server/rollbar.js @@ -12,6 +12,7 @@ var urllib = require('url'); var jsonBackup = require('json-stringify-safe'); var transforms = require('./transforms'); +var sharedTransforms = require('../transforms'); var predicates = require('./predicates'); function Rollbar(options, client) { @@ -362,9 +363,10 @@ function addTransformsToNotifier(notifier) { .addTransform(transforms.baseData) .addTransform(transforms.handleItemWithError) .addTransform(transforms.addBody) + .addTransform(sharedTransforms.addMessageWithError) .addTransform(transforms.addRequestData) .addTransform(transforms.scrubPayload) - .addTransform(transforms.convertToPayload); + .addTransform(sharedTransforms.itemToPayload); } function addPredicatesToQueue(queue) { diff --git a/sdks/rollbar.js/src/server/transforms.js b/sdks/rollbar.js/src/server/transforms.js index d81de9865..6dcea7d7f 100644 --- a/sdks/rollbar.js/src/server/transforms.js +++ b/sdks/rollbar.js/src/server/transforms.js @@ -154,16 +154,6 @@ function scrubPayload(item, options, callback) { callback(null, item); } -function convertToPayload(item, options, callback) { - var payloadOptions = options.payload || {}; - if (payloadOptions.body) { - delete payloadOptions.body; - } - - var data = _.extend(true, {}, item.data, payloadOptions); - callback(null, data); -} - /** Helpers **/ function _buildTraceData(chain) { @@ -231,7 +221,6 @@ module.exports = { addMessageData: addMessageData, addErrorData: addErrorData, addRequestData: addRequestData, - scrubPayload: scrubPayload, - convertToPayload: convertToPayload + scrubPayload: scrubPayload }; diff --git a/sdks/rollbar.js/src/transforms.js b/sdks/rollbar.js/src/transforms.js new file mode 100644 index 000000000..ba08554c5 --- /dev/null +++ b/sdks/rollbar.js/src/transforms.js @@ -0,0 +1,43 @@ +var _ = require('./utility'); + +function itemToPayload(item, options, callback) { + var payloadOptions = options.payload || {}; + if (payloadOptions.body) { + delete payloadOptions.body; + } + + var data = _.extend(true, {}, item.data, payloadOptions); + if (item._isUncaught) { + data._isUncaught = true; + } + callback(null, data); +} + +function addMessageWithError(item, options, callback) { + if (!item.message) { + callback(null, item); + return; + } + var tracePath = 'data.body.trace_chain.0'; + var trace = _.get(item, tracePath); + if (!trace) { + tracePath = 'data.body.trace'; + trace = _.get(item, tracePath); + } + if (trace) { + if (!(trace.exception && trace.exception.description)) { + _.set(item, tracePath+'.exception.description', item.message); + callback(null, item); + return; + } + var extra = _.get(item, tracePath+'.extra') || {}; + var newExtra = _.extend(true, {}, extra, {message: item.message}); + _.set(item, tracePath+'.extra', newExtra); + } + callback(null, item); +} + +module.exports = { + itemToPayload: itemToPayload, + addMessageWithError: addMessageWithError +}; diff --git a/sdks/rollbar.js/test/browser.transforms.test.js b/sdks/rollbar.js/test/browser.transforms.test.js index fe3e8cfaf..f3017e570 100644 --- a/sdks/rollbar.js/test/browser.transforms.test.js +++ b/sdks/rollbar.js/test/browser.transforms.test.js @@ -318,36 +318,6 @@ describe('addBody', function() { }); }); -describe('itemToPayload', function() { - it('ignores options.payload.body but merges in other payload options', function(done) { - var args = ['a message', {custom: 'stuff'}]; - var item = itemFromArgs(args); - item.accessToken = 'abc123'; - var options = { - endpoint: 'api.rollbar.com', - payload: {body: 'hey', x: 42} - }; - t.itemToPayload(item, options, function(e, i) { - expect(i.body).to.not.eql('hey'); - expect(i.x).to.eql(42); - done(e); - }); - }); - it('ignores handles trailing slash in endpoint', function(done) { - var args = ['a message', {custom: 'stuff'}]; - var item = itemFromArgs(args); - item.accessToken = 'abc123'; - item.data = {message: 'a message'}; - var options = { - endpoint: 'api.rollbar.com/' - }; - t.itemToPayload(item, options, function(e, i) { - expect(i.message).to.eql('a message'); - done(e); - }); - }); -}); - describe('scrubPayload', function() { it('only scrubs payload data', function(done) { var args = ['a message', {scooby: 'doo', okay: 'fizz=buzz&fuzz=baz', user: {id: 42}}]; diff --git a/sdks/rollbar.js/test/server.transforms.test.js b/sdks/rollbar.js/test/server.transforms.test.js index 77f99178d..f42b67bbb 100644 --- a/sdks/rollbar.js/test/server.transforms.test.js +++ b/sdks/rollbar.js/test/server.transforms.test.js @@ -679,37 +679,4 @@ vows.describe('transforms') } } }) - .addBatch({ - 'convertToPayload': { - 'options': { - 'with payload data': { - topic: function() { - return {payload: {client: {code_version: 'bork'}, body: 'hello'}}; - }, - item: { - topic: function(options) { - var item = { - data: { - body: { - message: 'hey' - } - }, - other: 'thing' - }; - t.convertToPayload(item, options, this.callback); - }, - 'should not error': function(err, item) { - assert.ifError(err); - }, - 'should only return data': function(err, item) { - assert.equal(item.body.message, 'hey'); - }, - 'should include payload options': function(err, item) { - assert.equal(item.client.code_version, 'bork'); - } - } - } - } - } - }) .export(module, {error: false}); diff --git a/sdks/rollbar.js/test/transforms.test.js b/sdks/rollbar.js/test/transforms.test.js new file mode 100644 index 000000000..bf71af0c1 --- /dev/null +++ b/sdks/rollbar.js/test/transforms.test.js @@ -0,0 +1,42 @@ +/* globals expect */ +/* globals describe */ +/* globals it */ + +var _ = require('../src/utility'); +var t = require('../src/transforms'); + +function itemFromArgs(args) { + var item = _.createItem(args); + item.level = 'debug'; + return item; +} + +describe('itemToPayload', function() { + it('ignores options.payload.body but merges in other payload options', function(done) { + var args = ['a message', {custom: 'stuff'}]; + var item = itemFromArgs(args); + item.accessToken = 'abc123'; + var options = { + endpoint: 'api.rollbar.com', + payload: {body: 'hey', x: 42} + }; + t.itemToPayload(item, options, function(e, i) { + expect(i.body).to.not.eql('hey'); + expect(i.x).to.eql(42); + done(e); + }); + }); + it('ignores handles trailing slash in endpoint', function(done) { + var args = ['a message', {custom: 'stuff'}]; + var item = itemFromArgs(args); + item.accessToken = 'abc123'; + item.data = {message: 'a message'}; + var options = { + endpoint: 'api.rollbar.com/' + }; + t.itemToPayload(item, options, function(e, i) { + expect(i.message).to.eql('a message'); + done(e); + }); + }); +});