Skip to content

Commit 73ffaf8

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 PR-URL: nodejs#7398 Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
1 parent 97e651d commit 73ffaf8

4 files changed

+42
-14
lines changed

src/node.cc

+18-13
Original file line numberDiff line numberDiff line change
@@ -1440,7 +1440,8 @@ ssize_t DecodeWrite(Isolate* isolate,
14401440

14411441
void AppendExceptionLine(Environment* env,
14421442
Local<Value> er,
1443-
Local<Message> message) {
1443+
Local<Message> message,
1444+
enum ErrorHandlingMode mode) {
14441445
if (message.IsEmpty())
14451446
return;
14461447

@@ -1522,19 +1523,23 @@ void AppendExceptionLine(Environment* env,
15221523

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

1525-
// Allocation failed, just print it out
1526-
if (arrow_str.IsEmpty() || err_obj.IsEmpty() || !err_obj->IsNativeError())
1527-
goto print;
1528-
1529-
err_obj->SetHiddenValue(env->arrow_message_string(), arrow_str);
1530-
return;
1526+
const bool can_set_arrow = !arrow_str.IsEmpty() && !err_obj.IsEmpty();
1527+
// If allocating arrow_str failed, print it out. There's not much else to do.
1528+
// If it's not an error, but something needs to be printed out because
1529+
// it's a fatal exception, also print it out from here.
1530+
// Otherwise, the arrow property will be attached to the object and handled
1531+
// by the caller.
1532+
if (!can_set_arrow || (mode == FATAL_ERROR && !err_obj->IsNativeError())) {
1533+
if (env->printed_error())
1534+
return;
1535+
env->set_printed_error(true);
15311536

1532-
print:
1533-
if (env->printed_error())
1537+
uv_tty_reset_mode();
1538+
PrintErrorString("\n%s", arrow);
15341539
return;
1535-
env->set_printed_error(true);
1536-
uv_tty_reset_mode();
1537-
PrintErrorString("\n%s", arrow);
1540+
}
1541+
1542+
err_obj->SetHiddenValue(env->arrow_message_string(), arrow_str);
15381543
}
15391544

15401545

@@ -1543,7 +1548,7 @@ static void ReportException(Environment* env,
15431548
Local<Message> message) {
15441549
HandleScope scope(env->isolate());
15451550

1546-
AppendExceptionLine(env, er, message);
1551+
AppendExceptionLine(env, er, message, FATAL_ERROR);
15471552

15481553
Local<Value> trace_value;
15491554
Local<Value> arrow;

src/node_internals.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,11 @@ constexpr size_t arraysize(const T(&)[N]) { return N; }
126126
# define NO_RETURN
127127
#endif
128128

129+
enum ErrorHandlingMode { FATAL_ERROR, CONTEXTIFY_ERROR };
129130
void AppendExceptionLine(Environment* env,
130131
v8::Local<v8::Value> er,
131-
v8::Local<v8::Message> message);
132+
v8::Local<v8::Message> message,
133+
enum ErrorHandlingMode mode = CONTEXTIFY_ERROR);
132134

133135
NO_RETURN void FatalError(const char* location, const char* message);
134136

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');
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)