Skip to content

Commit

Permalink
chore: diagnostic improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
waltjones committed May 8, 2020
1 parent 9b85887 commit 94cd515
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 4 deletions.
1 change: 0 additions & 1 deletion src/predicates.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ function checkLevel(item, settings) {
function userCheckIgnore(logger) {
return function(item, settings) {
var isUncaught = !!item._isUncaught;
delete item._isUncaught;
var args = item._originalArgs;
delete item._originalArgs;
try {
Expand Down
22 changes: 20 additions & 2 deletions src/transforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,22 @@ function addConfigToPayload(item, options, callback) {
callback(null, item);
}

function addFunctionOption(options, name) {
if(_.isFunction(options[name])) {
options[name] = options[name].toString();
}
}

function addConfiguredOptions(item, options, callback) {
delete options._configuredOptions.accessToken;
item.data.notifier.configured_options = options._configuredOptions;
var configuredOptions = options._configuredOptions;

// These must be stringified or they'll get dropped during serialization.
addFunctionOption(configuredOptions, 'transform');
addFunctionOption(configuredOptions, 'checkIgnore');
addFunctionOption(configuredOptions, 'onSendCallback');

delete configuredOptions.accessToken;
item.data.notifier.configured_options = configuredOptions;
callback(null, item);
}

Expand All @@ -88,6 +101,11 @@ function addDiagnosticKeys(item, options, callback) {
diagnostic.is_anonymous = true;
}

if (item._isUncaught) {
diagnostic.isUncaught = item._isUncaught;
delete item._isUncaught;
}

if (item.err) {
try {
diagnostic.raw_error = {
Expand Down
4 changes: 3 additions & 1 deletion src/utility.js
Original file line number Diff line number Diff line change
Expand Up @@ -413,11 +413,13 @@ function createItem(args, logger, notifier, requestKeys, lambdaContext) {
var arg;
var extraArgs = [];
var diagnostic = {};
var argTypes = [];

for (var i = 0, l = args.length; i < l; ++i) {
arg = args[i];

var typ = typeName(arg);
argTypes.push(typ);
switch (typ) {
case 'undefined':
break;
Expand Down Expand Up @@ -488,7 +490,7 @@ function createItem(args, logger, notifier, requestKeys, lambdaContext) {
if (lambdaContext) {
item.lambdaContext = lambdaContext;
}
item._originalArgs = args;
item.diagnostic.originalArgTypes = argTypes;
return item;
}

Expand Down
80 changes: 80 additions & 0 deletions test/browser.rollbar.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ describe('options.captureUncaught', function() {
expect(body.access_token).to.eql('POST_CLIENT_ITEM_TOKEN');
expect(body.data.body.trace.exception.message).to.eql('test error');
expect(body.data.notifier.diagnostic.raw_error.message).to.eql('test error');
expect(body.data.notifier.diagnostic.isUncaught).to.eql(true);

// karma doesn't unload the browser between tests, so the onerror handler
// will remain installed. Unset captureUncaught so the onerror handler
Expand Down Expand Up @@ -520,6 +521,7 @@ describe('options.captureUnhandledRejections', function() {

expect(body.access_token).to.eql('POST_CLIENT_ITEM_TOKEN');
expect(body.data.body.trace.exception.message).to.eql('test reject');
expect(body.data.notifier.diagnostic.isUncaught).to.eql(true);

rollbar.configure({
captureUnhandledRejections: false
Expand Down Expand Up @@ -617,6 +619,54 @@ describe('log', function() {
);
}

it('should send message when called with message and extra args', function(done) {
var server = window.server;
stubResponse(server);
server.requests.length = 0;

var options = {
accessToken: 'POST_CLIENT_ITEM_TOKEN'
};
var rollbar = window.rollbar = new Rollbar(options);

rollbar.log('test message', { 'foo': 'bar' });

server.respond();

var body = JSON.parse(server.requests[0].requestBody);

expect(body.data.body.message.body).to.eql('test message');
expect(body.data.body.message.extra).to.eql({ 'foo': 'bar' });
expect(body.data.notifier.diagnostic.isUncaught).to.eql(undefined);
expect(body.data.notifier.diagnostic.originalArgTypes).to.eql(['string', 'object']);

done();
})

it('should send exception when called with error and extra args', function(done) {
var server = window.server;
stubResponse(server);
server.requests.length = 0;

var options = {
accessToken: 'POST_CLIENT_ITEM_TOKEN'
};
var rollbar = window.rollbar = new Rollbar(options);

rollbar.log(new Error('test error'), { 'foo': 'bar' });

server.respond();

var body = JSON.parse(server.requests[0].requestBody);

expect(body.data.body.trace.exception.message).to.eql('test error');
expect(body.data.body.trace.extra).to.eql({ 'foo': 'bar' });
expect(body.data.notifier.diagnostic.isUncaught).to.eql(undefined);
expect(body.data.notifier.diagnostic.originalArgTypes).to.eql(['error', 'object']);

done();
})

it('should send message when called with only null arguments', function(done) {
var server = window.server;
stubResponse(server);
Expand All @@ -635,6 +685,7 @@ describe('log', function() {
var body = JSON.parse(server.requests[0].requestBody);

expect(body.data.body.message.body).to.eql('Item sent with null or missing arguments.');
expect(body.data.notifier.diagnostic.originalArgTypes).to.eql(['null']);

done();
})
Expand Down Expand Up @@ -756,6 +807,31 @@ describe('callback options', function() {
done();
});

it('should send when checkIgnore returns false', function(done) {
var server = window.server;
stubResponse(server);
server.requests.length = 0;

var options = {
accessToken: 'POST_CLIENT_ITEM_TOKEN',
checkIgnore: function(_isUncaught, _args, _payload) {
return false;
}
};
var rollbar = window.rollbar = new Rollbar(options);

rollbar.log('test'); // generate a payload to inspect

server.respond();

expect(server.requests.length).to.eql(1);
var body = JSON.parse(server.requests[0].requestBody);
expect(body.data.notifier.configured_options.checkIgnore.substr(0,8))
.to.eql('function');

done();
});

it('should use onSendCallback when set', function(done) {
var server = window.server;
stubResponse(server);
Expand All @@ -776,6 +852,8 @@ describe('callback options', function() {
expect(server.requests.length).to.eql(1);
var body = JSON.parse(server.requests[0].requestBody);
expect(body.data.foo).to.eql('bar');
expect(body.data.notifier.configured_options.onSendCallback.substr(0,8))
.to.eql('function');

done();
});
Expand All @@ -800,6 +878,8 @@ describe('callback options', function() {
expect(server.requests.length).to.eql(1);
var body = JSON.parse(server.requests[0].requestBody);
expect(body.data.foo).to.eql('baz');
expect(body.data.notifier.configured_options.transform.substr(0,8))
.to.eql('function');

done();
});
Expand Down

0 comments on commit 94cd515

Please sign in to comment.