Skip to content

Commit 3cac616

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: #7397 PR-URL: #7398 Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
1 parent 02afb05 commit 3cac616

File tree

5 files changed

+45
-15
lines changed

5 files changed

+45
-15
lines changed

src/node.cc

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1460,7 +1460,8 @@ bool IsExceptionDecorated(Environment* env, Local<Value> er) {
14601460

14611461
void AppendExceptionLine(Environment* env,
14621462
Local<Value> er,
1463-
Local<Message> message) {
1463+
Local<Message> message,
1464+
enum ErrorHandlingMode mode) {
14641465
if (message.IsEmpty())
14651466
return;
14661467

@@ -1547,20 +1548,26 @@ void AppendExceptionLine(Environment* env,
15471548

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

1550-
if (!arrow_str.IsEmpty() && !err_obj.IsEmpty() && err_obj->IsNativeError()) {
1551-
err_obj->SetPrivate(
1552-
env->context(),
1553-
env->arrow_message_private_symbol(),
1554-
arrow_str);
1551+
const bool can_set_arrow = !arrow_str.IsEmpty() && !err_obj.IsEmpty();
1552+
// If allocating arrow_str failed, print it out. There's not much else to do.
1553+
// If it's not an error, but something needs to be printed out because
1554+
// it's a fatal exception, also print it out from here.
1555+
// Otherwise, the arrow property will be attached to the object and handled
1556+
// by the caller.
1557+
if (!can_set_arrow || (mode == FATAL_ERROR && !err_obj->IsNativeError())) {
1558+
if (env->printed_error())
1559+
return;
1560+
env->set_printed_error(true);
1561+
1562+
uv_tty_reset_mode();
1563+
PrintErrorString("\n%s", arrow);
15551564
return;
15561565
}
15571566

1558-
// Allocation failed, just print it out.
1559-
if (env->printed_error())
1560-
return;
1561-
env->set_printed_error(true);
1562-
uv_tty_reset_mode();
1563-
PrintErrorString("\n%s", arrow);
1567+
CHECK(err_obj->SetPrivate(
1568+
env->context(),
1569+
env->arrow_message_private_symbol(),
1570+
arrow_str).FromMaybe(false));
15641571
}
15651572

15661573

@@ -1569,7 +1576,7 @@ static void ReportException(Environment* env,
15691576
Local<Message> message) {
15701577
HandleScope scope(env->isolate());
15711578

1572-
AppendExceptionLine(env, er, message);
1579+
AppendExceptionLine(env, er, message, FATAL_ERROR);
15731580

15741581
Local<Value> trace_value;
15751582
Local<Value> arrow;

src/node_contextify.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,7 @@ class ContextifyScript : public BaseObject {
633633
if (IsExceptionDecorated(env, err_obj))
634634
return;
635635

636-
AppendExceptionLine(env, exception, try_catch.Message());
636+
AppendExceptionLine(env, exception, try_catch.Message(), CONTEXTIFY_ERROR);
637637
Local<Value> stack = err_obj->Get(env->stack_string());
638638
auto maybe_value =
639639
err_obj->GetPrivate(

src/node_internals.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,11 @@ constexpr size_t arraysize(const T(&)[N]) { return N; }
135135

136136
bool IsExceptionDecorated(Environment* env, v8::Local<v8::Value> er);
137137

138+
enum ErrorHandlingMode { FATAL_ERROR, CONTEXTIFY_ERROR };
138139
void AppendExceptionLine(Environment* env,
139140
v8::Local<v8::Value> er,
140-
v8::Local<v8::Message> message);
141+
v8::Local<v8::Message> message,
142+
enum ErrorHandlingMode mode);
141143

142144
NO_RETURN void FatalError(const char* location, const char* message);
143145

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
// vm.runInThisContext() should not print out anything to stderr by itself.
9+
try {
10+
vm.runInThisContext(`throw ({
11+
name: 'MyCustomError',
12+
message: 'This is a custom message'
13+
})`, { filename: 'test.vm' });
14+
} catch (e) {
15+
console.error('received error', e.name);
16+
}
17+
18+
console.error('end');
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
beginning
2+
received error MyCustomError
3+
end

0 commit comments

Comments
 (0)