Skip to content
This repository has been archived by the owner on Oct 23, 2023. It is now read-only.

Commit

Permalink
fix: correctly detect errors from vm (#457)
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilogorek authored May 8, 2018
1 parent 381dd6f commit 46c7a51
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ extend(Raven.prototype, {
kwargs = utils.isPlainObject(kwargs) ? extend({}, kwargs) : {};
}

if (!(err instanceof Error)) {
if (!utils.isError(err)) {
if (utils.isPlainObject(err)) {
// This will allow us to group events based on top-level keys
// which is much better than creating new group when any key/value change
Expand Down Expand Up @@ -451,7 +451,7 @@ extend(Raven.prototype, {
var self = this;
var wrapped = function() {
var err = arguments[0];
if (err instanceof Error) {
if (utils.isError(err)) {
self.captureException(err, options);
} else {
func.apply(null, arguments);
Expand Down
8 changes: 8 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ function jsonSize(value) {
return utf8Length(JSON.stringify(value));
}

function isError(what) {
return (
Object.prototype.toString.call(what) === '[object Error]' || what instanceof Error
);
}

module.exports.isError = isError;

function isPlainObject(what) {
return Object.prototype.toString.call(what) === '[object Object]';
}
Expand Down
33 changes: 33 additions & 0 deletions test/raven.utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -566,4 +566,37 @@ describe('raven.utils', function() {
raven.utils.serializeKeysForMessage('foo').should.eql('foo');
});
});

describe('isError', function() {
it('should work as advertised', function() {
function RavenError(message) {
this.name = 'RavenError';
this.message = message;
}
RavenError.prototype = new Error();
RavenError.prototype.constructor = RavenError;

raven.utils.isError(new Error()).should.be.true;
raven.utils.isError(new RavenError()).should.be.true;
raven.utils.isError({}).should.be.false;
raven.utils.isError({
message: 'A fake error',
stack: 'no stack here'
}).should.be.false;
raven.utils.isError('').should.be.false;
raven.utils.isError(true).should.be.false;
});

it('should work with errors from different contexts, eg. vm module', function(done) {
var vm = require('vm');
var script = new vm.Script("throw new Error('this is the error')");

try {
script.runInNewContext();
} catch (e) {
raven.utils.isError(e).should.be.true;
done();
}
});
});
});

0 comments on commit 46c7a51

Please sign in to comment.