Skip to content

Commit 7af968a

Browse files
committed
vm: don't print out arrow message for custom error
In `AppendExceptionLine()`, which is used both by the `vm` module and the uncaught exception handler, don’t print anything to stderr when called from the `vm` module, even if the thrown object is not a native error instance. Fixes: nodejs#7397
1 parent 5267f29 commit 7af968a

File tree

4 files changed

+27
-5
lines changed

4 files changed

+27
-5
lines changed

src/node.cc

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1423,7 +1423,8 @@ bool IsExceptionDecorated(Environment* env, Local<Value> er) {
14231423

14241424
void AppendExceptionLine(Environment* env,
14251425
Local<Value> er,
1426-
Local<Message> message) {
1426+
Local<Message> message,
1427+
bool handlingFatalError) {
14271428
if (message.IsEmpty())
14281429
return;
14291430

@@ -1510,15 +1511,16 @@ void AppendExceptionLine(Environment* env,
15101511

15111512
Local<String> arrow_str = String::NewFromUtf8(env->isolate(), arrow);
15121513

1513-
if (!arrow_str.IsEmpty() && !err_obj.IsEmpty() && err_obj->IsNativeError()) {
1514+
if (!arrow_str.IsEmpty() && !err_obj.IsEmpty() &&
1515+
(!handlingFatalError || err_obj->IsNativeError())) {
15141516
err_obj->SetPrivate(
15151517
env->context(),
15161518
env->arrow_message_private_symbol(),
15171519
arrow_str);
15181520
return;
15191521
}
15201522

1521-
// Allocation failed, just print it out.
1523+
// Allocation failed when called from ReportException(), just print it out.
15221524
if (env->printed_error())
15231525
return;
15241526
env->set_printed_error(true);
@@ -1532,7 +1534,7 @@ static void ReportException(Environment* env,
15321534
Local<Message> message) {
15331535
HandleScope scope(env->isolate());
15341536

1535-
AppendExceptionLine(env, er, message);
1537+
AppendExceptionLine(env, er, message, true);
15361538

15371539
Local<Value> trace_value;
15381540
Local<Value> arrow;

src/node_internals.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,8 @@ bool IsExceptionDecorated(Environment* env, v8::Local<v8::Value> er);
139139

140140
void AppendExceptionLine(Environment* env,
141141
v8::Local<v8::Value> er,
142-
v8::Local<v8::Message> message);
142+
v8::Local<v8::Message> message,
143+
bool handlingFatalError = false);
143144

144145
NO_RETURN void FatalError(const char* location, const char* message);
145146

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'use strict';
2+
require('../common');
3+
const vm = require('vm');
4+
5+
console.error('beginning');
6+
7+
// Regression test for https://github.com/nodejs/node/issues/7397:
8+
// This should not print out anything to stderr due to the error being caught.
9+
try {
10+
vm.runInThisContext(`throw ({
11+
name: 'MyCustomError',
12+
message: 'This is a custom message'
13+
})`, { filename: 'test.vm' });
14+
} catch (e) {
15+
}
16+
17+
console.error('end');
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
beginning
2+
end

0 commit comments

Comments
 (0)