Closed
Description
- Version: v6.2.2
- Platform: Ubuntu 16.04
- Subsystem: vm
I was playing around with dynamically running code and found a difference between standard JS eval and the vm module with custom error code. Here is a code snippet that can be run
var vm = require("vm");
var customError = `
function MyError(message) {
this.name = this.constructor.name;
this.message = message;
};
MyError.prototype = Error.prototype;
`;
var errorThrower = "throw new MyError('MyError');";
console.log("Eval - MyError");
try {
var result = eval(customError + "\n" + errorThrower);
} catch (e) {
console.log(e.message + "\n");
}
console.log("Good VM - Error");
try {
var result = vm.runInThisContext("throw new Error('Not MyError');", "repl");
} catch (e) {
console.log(e.message + "\n");
}
console.log("Bad VM - MyError");
try {
var result = vm.runInThisContext(customError + "\n" + errorThrower, "repl");
} catch (e) {
console.log(e.message + "\n");
}
console.log("Bad VM - MyError Again");
try {
var result = vm.runInThisContext(customError + "\n" + errorThrower, "repl");
} catch (e) {
console.log(e.message + "\n");
}
and the output
kamn@kamn:~/Dev/VmEvalTest$ node index.js
Eval - MyError
MyError
Good VM - Error
Not MyError
Bad VM - MyError
repl:7
throw new MyError('MyError');
^
MyError
Bad VM - MyError Again
MyError
It seems like custom errors in vm output to stdin(?) the first time they happen. They are even caught afterwards. This is not true for a standard Error in vm and both Errors work in JS Eval. I am not sure this is a expected behavior but it does not seem like it. Any feedback would be appreciated.
Thanks!