Skip to content

Commit 565c556

Browse files
laverdetMylesBorins
authored andcommitted
vm: fix displayErrors in runIn.. functions
This option has been broken for almost a year when used with any of the vm.runIn.. family of functions, except for syntax errors. Backport-PR-URL: #14373 PR-URL: #13074 Reviewed-By: Anna Henningsen <[email protected]>
1 parent 97f5806 commit 565c556

File tree

4 files changed

+30
-13
lines changed

4 files changed

+30
-13
lines changed

lib/repl.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ function REPLServer(prompt,
332332
try {
333333
try {
334334
const scriptOptions = {
335-
displayErrors: true,
335+
displayErrors: false,
336336
breakOnSigint: self.breakEvalOnSigint
337337
};
338338

src/node_contextify.cc

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -684,8 +684,10 @@ class ContextifyScript : public BaseObject {
684684
return;
685685
}
686686

687-
Local<String> decorated_stack = String::Concat(arrow.As<String>(),
688-
stack.As<String>());
687+
Local<String> decorated_stack = String::Concat(
688+
String::Concat(arrow.As<String>(),
689+
FIXED_ONE_BYTE_STRING(env->isolate(), "\n")),
690+
stack.As<String>());
689691
err_obj->Set(env->stack_string(), decorated_stack);
690692
err_obj->SetPrivate(
691693
env->context(),
@@ -932,6 +934,9 @@ class ContextifyScript : public BaseObject {
932934
env->ThrowError("Script execution timed out.");
933935
} else if (received_signal) {
934936
env->ThrowError("Script execution interrupted.");
937+
} else if (display_errors) {
938+
// We should decorate non-termination exceptions
939+
DecorateErrorStack(env, *try_catch);
935940
}
936941

937942
// If there was an exception thrown during script execution, re-throw it.
@@ -944,15 +949,6 @@ class ContextifyScript : public BaseObject {
944949
return false;
945950
}
946951

947-
if (result.IsEmpty()) {
948-
// Error occurred during execution of the script.
949-
if (display_errors) {
950-
DecorateErrorStack(env, *try_catch);
951-
}
952-
try_catch->ReThrow();
953-
return false;
954-
}
955-
956952
args.GetReturnValue().Set(result);
957953
return true;
958954
}

test/message/vm_display_runtime_error.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ const vm = require('vm');
44

55
console.error('beginning');
66

7-
vm.runInThisContext('throw new Error("boo!")', { filename: 'test.vm' });
7+
try {
8+
vm.runInThisContext('throw new Error("boo!")', { filename: 'test.vm'});
9+
} catch (err) {
10+
console.error(err.stack);
11+
}
12+
13+
vm.runInThisContext('throw new Error("spooky!")', { filename: 'test.vm' });
814

915
console.error('end');

test/message/vm_display_runtime_error.out

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,18 @@ Error: boo!
1414
at tryModuleLoad (module.js:*:*)
1515
at Function.Module._load (module.js:*)
1616
at Module.runMain (module.js:*)
17+
test.vm:1
18+
throw new Error("spooky!")
19+
^
20+
21+
Error: spooky!
22+
at test.vm:1:7
23+
at ContextifyScript.Script.runInThisContext (vm.js:*)
24+
at Object.runInThisContext (vm.js:*)
25+
at Object.<anonymous> (*test*message*vm_display_runtime_error.js:*)
26+
at Module._compile (module.js:*)
27+
at Object.Module._extensions..js (module.js:*)
28+
at Module.load (module.js:*)
29+
at tryModuleLoad (module.js:*:*)
30+
at Function.Module._load (module.js:*)
31+
at Module.runMain (module.js:*)

0 commit comments

Comments
 (0)