Skip to content

Commit 46c7a51

Browse files
authored
fix: correctly detect errors from vm (#457)
1 parent 381dd6f commit 46c7a51

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

lib/client.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ extend(Raven.prototype, {
366366
kwargs = utils.isPlainObject(kwargs) ? extend({}, kwargs) : {};
367367
}
368368

369-
if (!(err instanceof Error)) {
369+
if (!utils.isError(err)) {
370370
if (utils.isPlainObject(err)) {
371371
// This will allow us to group events based on top-level keys
372372
// which is much better than creating new group when any key/value change
@@ -451,7 +451,7 @@ extend(Raven.prototype, {
451451
var self = this;
452452
var wrapped = function() {
453453
var err = arguments[0];
454-
if (err instanceof Error) {
454+
if (utils.isError(err)) {
455455
self.captureException(err, options);
456456
} else {
457457
func.apply(null, arguments);

lib/utils.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ function jsonSize(value) {
3131
return utf8Length(JSON.stringify(value));
3232
}
3333

34+
function isError(what) {
35+
return (
36+
Object.prototype.toString.call(what) === '[object Error]' || what instanceof Error
37+
);
38+
}
39+
40+
module.exports.isError = isError;
41+
3442
function isPlainObject(what) {
3543
return Object.prototype.toString.call(what) === '[object Object]';
3644
}

test/raven.utils.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,4 +566,37 @@ describe('raven.utils', function() {
566566
raven.utils.serializeKeysForMessage('foo').should.eql('foo');
567567
});
568568
});
569+
570+
describe('isError', function() {
571+
it('should work as advertised', function() {
572+
function RavenError(message) {
573+
this.name = 'RavenError';
574+
this.message = message;
575+
}
576+
RavenError.prototype = new Error();
577+
RavenError.prototype.constructor = RavenError;
578+
579+
raven.utils.isError(new Error()).should.be.true;
580+
raven.utils.isError(new RavenError()).should.be.true;
581+
raven.utils.isError({}).should.be.false;
582+
raven.utils.isError({
583+
message: 'A fake error',
584+
stack: 'no stack here'
585+
}).should.be.false;
586+
raven.utils.isError('').should.be.false;
587+
raven.utils.isError(true).should.be.false;
588+
});
589+
590+
it('should work with errors from different contexts, eg. vm module', function(done) {
591+
var vm = require('vm');
592+
var script = new vm.Script("throw new Error('this is the error')");
593+
594+
try {
595+
script.runInNewContext();
596+
} catch (e) {
597+
raven.utils.isError(e).should.be.true;
598+
done();
599+
}
600+
});
601+
});
569602
});

0 commit comments

Comments
 (0)