Skip to content

Commit

Permalink
Merge pull request #313 from rollbar/fix-server-message-and-error
Browse files Browse the repository at this point in the history
handle messages and errors on the server side
  • Loading branch information
rokob authored Jun 16, 2017
2 parents 07cac7a + b051cea commit 7c3ba4c
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 21 deletions.
4 changes: 2 additions & 2 deletions sdks/rollbar.js/src/server/rollbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
40 changes: 29 additions & 11 deletions sdks/rollbar.js/src/server/transforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,31 @@ function baseData(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 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);
return;
Expand All @@ -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);
};
Expand Down Expand Up @@ -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
Expand Down
59 changes: 51 additions & 8 deletions sdks/rollbar.js/test/server.transforms.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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': {
Expand Down Expand Up @@ -195,7 +238,7 @@ vows.describe('transforms')
}
})
.addBatch({
'buildErrorData': {
'handleItemWithError': {
'options': {
'anything': {
topic: function() {
Expand All @@ -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);
Expand All @@ -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': {
Expand All @@ -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': {
Expand All @@ -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');
Expand Down

0 comments on commit 7c3ba4c

Please sign in to comment.