Skip to content

Eval and vm module with custom errors #7397

Description

@kamn
  • 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

varvm=require("vm");varcustomError=`function MyError(message) { this.name = this.constructor.name; this.message = message;};MyError.prototype = Error.prototype;`;varerrorThrower="throw new MyError('MyError');";console.log("Eval - MyError");try{varresult=eval(customError+"\n"+errorThrower);}catch(e){console.log(e.message+"\n");}console.log("Good VM - Error");try{varresult=vm.runInThisContext("throw new Error('Not MyError');","repl");}catch(e){console.log(e.message+"\n");}console.log("Bad VM - MyError");try{varresult=vm.runInThisContext(customError+"\n"+errorThrower,"repl");}catch(e){console.log(e.message+"\n");}console.log("Bad VM - MyError Again");try{varresult=vm.runInThisContext(customError+"\n"+errorThrower,"repl");}catch(e){console.log(e.message+"\n");}

and the output

kamn@kamn:~/Dev/VmEvalTest$ node index.js Eval - MyErrorMyErrorGood VM - ErrorNot MyErrorBad VM - MyErrorrepl:7throw new MyError('MyError');^MyErrorBad VM - MyError AgainMyError

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!

Metadata

Metadata

Assignees

Labels

vmIssues and PRs related to the vm subsystem.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions