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

fix: correctly detect errors from vm #457

Merged
merged 1 commit into from
May 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}
});
});
});