From 2425b4403890387d65895811b88a056115b109f2 Mon Sep 17 00:00:00 2001 From: Andrew Ledvina Date: Thu, 15 Jun 2017 12:19:14 -0700 Subject: [PATCH 1/2] handle messages and errors on the server side --- sdks/rollbar.js/src/server/rollbar.js | 4 +- sdks/rollbar.js/src/server/transforms.js | 40 +++++++++---- .../rollbar.js/test/server.transforms.test.js | 59 ++++++++++++++++--- 3 files changed, 82 insertions(+), 21 deletions(-) diff --git a/sdks/rollbar.js/src/server/rollbar.js b/sdks/rollbar.js/src/server/rollbar.js index 18b28db55..9a67d5f9c 100644 --- a/sdks/rollbar.js/src/server/rollbar.js +++ b/sdks/rollbar.js/src/server/rollbar.js @@ -322,8 +322,8 @@ Rollbar.handleUncaughtExceptionsAndRejections = function(accessToken, options) { function addTransformsToNotifier(notifier) { notifier .addTransform(transforms.baseData) - .addTransform(transforms.addMessageData) - .addTransform(transforms.buildErrorData) + .addTransform(transforms.handleItemWithError) + .addTransform(transforms.addBody) .addTransform(transforms.addRequestData) .addTransform(transforms.scrubPayload) .addTransform(transforms.convertToPayload); diff --git a/sdks/rollbar.js/src/server/transforms.js b/sdks/rollbar.js/src/server/transforms.js index 9dd4de209..fc45d542b 100644 --- a/sdks/rollbar.js/src/server/transforms.js +++ b/sdks/rollbar.js/src/server/transforms.js @@ -43,18 +43,34 @@ function baseData(item, options, callback) { callback(null, item); } +function addBody(item, options, callback) { + if (item.stackInfo) { + addErrorData(item, options, callback); + } else { + addMessageData(item, options, callback); + } +} + function addMessageData(item, options, callback) { item.data = item.data || {}; item.data.body = item.data.body || {}; - if (item.message !== undefined) { - item.data.body.message = { - body: item.message - }; + var message = item.message || ''; + item.data.body.message = { + body: message + }; + callback(null, item); +} + +function addErrorData(item, options, callback) { + if (item.stackInfo) { + item.data = item.data || {}; + item.data.body = item.data.body || {}; + item.data.body.trace_chain = item.stackInfo; } callback(null, item); } -function buildErrorData(item, options, callback) { +function handleItemWithError(item, options, callback) { if (!item.err) { callback(null, item); return; @@ -66,14 +82,14 @@ function buildErrorData(item, options, callback) { do { errors.push(err); } while ((err = err.nested) !== undefined); - - item.data = item.data || {}; - item.data.body = item.data.body || {}; - item.data.body.trace_chain = chain; + item.stackInfo = chain; var cb = function(err) { if (err) { - callback(err, null); + item.message = item.err.message || item.err.description || item.message || String(item.err); + delete item.err; + delete item.stackInfo; + callback(null, item); } callback(null, item); }; @@ -203,8 +219,10 @@ function _buildRequestData(req) { module.exports = { baseData: baseData, + handleItemWithError: handleItemWithError, + addBody: addBody, addMessageData: addMessageData, - buildErrorData: buildErrorData, + addErrorData: addErrorData, addRequestData: addRequestData, scrubPayload: scrubPayload, convertToPayload: convertToPayload diff --git a/sdks/rollbar.js/test/server.transforms.test.js b/sdks/rollbar.js/test/server.transforms.test.js index 498014b9c..9814a064b 100644 --- a/sdks/rollbar.js/test/server.transforms.test.js +++ b/sdks/rollbar.js/test/server.transforms.test.js @@ -157,6 +157,49 @@ vows.describe('transforms') } } }) + .addBatch({ + 'addBody': { + 'options': { + 'anything': { + topic: function() { + return {whatever: 'stuff'}; + }, + 'item': { + 'with stackInfo': { + topic: function(options) { + var item = {stackInfo: [{message: 'hey'}]}; + t.addBody(item, options, this.callback); + }, + 'should not error': function(err, item) { + assert.ifError(err); + }, + 'should set the trace_chain': function(err, item) { + assert.ok(item.data.body.trace_chain); + }, + 'should not set a message': function(err, item) { + assert.ok(!item.data.body.message); + } + }, + 'with no stackInfo': { + topic: function(options) { + var item = {message: 'hello'}; + t.addBody(item, options, this.callback); + }, + 'should not error': function(err, item) { + assert.ifError(err); + }, + 'should not set the trace_chain': function(err, item) { + assert.ok(!item.data.body.trace_chain); + }, + 'should set a message': function(err, item) { + assert.ok(item.data.body.message); + } + } + } + } + } + } + }) .addBatch({ 'addMessageData': { 'options': { @@ -195,7 +238,7 @@ vows.describe('transforms') } }) .addBatch({ - 'buildErrorData': { + 'handleItemWithError': { 'options': { 'anything': { topic: function() { @@ -208,7 +251,7 @@ vows.describe('transforms') data: {body: {yo: 'hey'}}, message: 'hey' }; - t.buildErrorData(item, options, this.callback); + t.handleItemWithError(item, options, this.callback); }, 'should not error': function(err, item) { assert.ifError(err); @@ -223,13 +266,13 @@ vows.describe('transforms') data: {body: {}}, err: new Error('wookie') }; - t.buildErrorData(item, options, this.callback); + t.handleItemWithError(item, options, this.callback); }, 'should not error': function(err, item) { assert.ifError(err); }, 'should add some data to the trace_chain': function(err, item) { - assert.ok(item.data.body.trace_chain); + assert.ok(item.stackInfo); } }, 'with a normal error': { @@ -247,13 +290,13 @@ vows.describe('transforms') data: {body: {}}, err: err }; - t.buildErrorData(item, options, this.callback); + t.handleItemWithError(item, options, this.callback); }, 'should not error': function(err, item) { assert.ifError(err); }, 'should add some data to the trace_chain': function(err, item) { - assert.ok(item.data.body.trace_chain); + assert.ok(item.stackInfo); } }, 'with a nested error': { @@ -271,13 +314,13 @@ vows.describe('transforms') data: {body: {}}, err: err }; - t.buildErrorData(item, options, this.callback); + t.handleItemWithError(item, options, this.callback); }, 'should not error': function(err, item) { assert.ifError(err); }, 'should have the right data in the trace_chain': function(err, item) { - var trace_chain = item.data.body.trace_chain; + var trace_chain = item.stackInfo; assert.lengthOf(trace_chain, 2); assert.equal(trace_chain[0].exception.class, 'CustomError'); assert.equal(trace_chain[0].exception.message, 'nested-message'); From b051ceaf6c5cc03d0d5171e85cb9286000aa47fc Mon Sep 17 00:00:00 2001 From: Andrew Ledvina Date: Thu, 15 Jun 2017 12:40:47 -0700 Subject: [PATCH 2/2] satisfy codacy even though it is wrong --- sdks/rollbar.js/src/server/transforms.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/sdks/rollbar.js/src/server/transforms.js b/sdks/rollbar.js/src/server/transforms.js index fc45d542b..6dfb3fb7b 100644 --- a/sdks/rollbar.js/src/server/transforms.js +++ b/sdks/rollbar.js/src/server/transforms.js @@ -43,14 +43,6 @@ function baseData(item, options, callback) { callback(null, item); } -function addBody(item, options, callback) { - if (item.stackInfo) { - addErrorData(item, options, callback); - } else { - addMessageData(item, options, callback); - } -} - function addMessageData(item, options, callback) { item.data = item.data || {}; item.data.body = item.data.body || {}; @@ -70,6 +62,14 @@ function addErrorData(item, options, callback) { callback(null, item); } +function addBody(item, options, callback) { + if (item.stackInfo) { + addErrorData(item, options, callback); + } else { + addMessageData(item, options, callback); + } +} + function handleItemWithError(item, options, callback) { if (!item.err) { callback(null, item);