Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle errors thrown in async functions #213

Closed
Closed
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
51 changes: 47 additions & 4 deletions lib/sandbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,11 @@ return ((vm, host) => {

global.setTimeout = (callback, delay, ...args) => {
const tmr = host.setTimeout(Decontextify.value(() => {
callback(...args);
try {
callback(...args);
} catch (err) {
vm.emit('uncaughtException', err);
}
}), Decontextify.value(delay));

const local = {
Expand All @@ -347,7 +351,11 @@ return ((vm, host) => {

global.setInterval = (callback, interval, ...args) => {
const tmr = host.setInterval(Decontextify.value(() => {
callback(...args);
try {
callback(...args);
} catch (err) {
vm.emit('uncaughtException', err);
}
}), Decontextify.value(interval));

const local = {
Expand All @@ -361,7 +369,11 @@ return ((vm, host) => {

global.setImmediate = (callback, ...args) => {
const tmr = host.setImmediate(Decontextify.value(() => {
callback(...args);
try {
callback(...args);
} catch (err) {
vm.emit('uncaughtException', err);
}
}));

const local = {
Expand All @@ -388,6 +400,33 @@ return ((vm, host) => {
return null;
};

global.Promise = function _Promise(fn) {
const wrapFn = (resolve, reject) => {
const _reject = reason => {
try {
reject(reason);
} catch (err) {
vm.emit('unhandledRejection', err);
}
};
try {
fn(resolve, _reject);
} catch (err) {
vm.emit('unhandledRejection', err);
}
};
const p = new host.Promise(wrapFn);
// prevents unhandledRejection errors from propagating,
// but we don't know if it is ever properly handled or not
p.catch(err => vm.emit('promiseRejected', err, p));
return p;
};
global.Promise.prototype = host.Promise.prototype;
global.Promise.all = host.Promise.all;
global.Promise.race = host.Promise.race;
global.Promise.reject = host.Promise.reject;
global.Promise.resolve = host.Promise.resolve;

global.process = {
argv: [],
title: host.process.title,
Expand All @@ -405,7 +444,11 @@ return ((vm, host) => {

try {
return host.process.nextTick(Decontextify.value(() => {
callback(...args);
try {
callback(...args);
} catch (err) {
vm.emit('uncaughtException', err);
}
}));
} catch (e) {
throw Contextify.value(e);
Expand Down
29 changes: 29 additions & 0 deletions test/nodevm.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,35 @@ describe('NodeVM', () => {
});
});

describe('error events', () => {
it('async errors', done => {
const vm = new NodeVM;
vm.on('uncaughtException', err => {
assert.equal(err.message, 'fail');
done();
});
vm.run('setTimeout(function() { throw new Error("fail"); })');
});

it('promise errors', done => {
const vm = new NodeVM;
vm.on('unhandledRejection', err => {
assert.equal(err.message, 'fail');
done();
});
vm.run('new Promise(function() { throw new Error("fail"); })');
});

it('rejected promises', done => {
const vm = new NodeVM;
vm.on('promiseRejected', err => {
assert.equal(err.message, 'fail');
done();
});
vm.run('new Promise(function(resolve, reject) { reject(new Error("fail")); })');
});
});

describe('modules', () => {
it('require json', () => {
const vm = new NodeVM({
Expand Down