Skip to content

Commit

Permalink
Merge pull request #781 from rollbar/wj-most-specific-error-name
Browse files Browse the repository at this point in the history
Use most specific error name
  • Loading branch information
waltjones authored Sep 12, 2019
2 parents 1661245 + 2fd641b commit 961f58d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 6 deletions.
23 changes: 17 additions & 6 deletions sdks/rollbar.js/src/browser/errorParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,10 @@ function Stack(exception) {
return stack;
}

var name = exception.constructor && exception.constructor.name;
if (!name || !name.length || name.length < 3) {
name = exception.name;
}

return {
stack: getStack(),
message: exception.message,
name: name,
name: _mostSpecificErrorName(exception),
rawStack: exception.stack,
rawException: exception
};
Expand Down Expand Up @@ -109,6 +104,22 @@ function guessErrorClass(errMsg) {
return [errClass, errMsg];
}

// * Prefers any value over an empty string
// * Prefers any value over 'Error' where possible
// * Prefers name over constructor.name when both are more specific than 'Error'
function _mostSpecificErrorName(error) {
var name = error.name && error.name.length && error.name;
var constructorName = error.constructor.name && error.constructor.name.length && error.constructor.name;

if (!name || !constructorName) {
return name || constructorName;
}

if (name === 'Error') {
return constructorName;
}
return name;
}

module.exports = {
guessFunctionName: guessFunctionName,
Expand Down
26 changes: 26 additions & 0 deletions sdks/rollbar.js/test/browser.transforms.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,32 @@ describe('handleItemWithError', function() {
done(e);
});
});
it('should use most specific error name', function(done) {
var err = new Error('bork');
var args = ['a message', err];
var options = {};

var names = [
{name: 'TypeError', constructor: 'EvalError', result: 'TypeError'},
{name: 'TypeError', constructor: 'Error', result: 'TypeError'},
{name: 'Error', constructor: 'TypeError', result: 'TypeError'},
{name: 'Error', constructor: '', result: 'Error'},
{name: '', constructor: 'Error', result: 'Error'},
{name: '', constructor: '', result: ''}
];

for(var i = 0; i < names.length; i++) {
err.name = names[i].name;
err.constructor = { name: names[i].constructor };
var item = itemFromArgs(args);
var result = names[i].result;

t.handleItemWithError(item, options, function(e, i) {
expect(i.stackInfo.name).to.eql(result);
});
};
done();
});
});

describe('ensureItemHasSomethingToSay', function() {
Expand Down

0 comments on commit 961f58d

Please sign in to comment.