Skip to content

Commit

Permalink
vm: don't print out arrow message for custom error
Browse files Browse the repository at this point in the history
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
  • Loading branch information
addaleax committed Jun 24, 2016
1 parent 5267f29 commit 7af968a
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 5 deletions.
10 changes: 6 additions & 4 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1423,7 +1423,8 @@ bool IsExceptionDecorated(Environment* env, Local<Value> er) {

void AppendExceptionLine(Environment* env,
Local<Value> er,
Local<Message> message) {
Local<Message> message,
bool handlingFatalError) {
if (message.IsEmpty())
return;

Expand Down Expand Up @@ -1510,15 +1511,16 @@ void AppendExceptionLine(Environment* env,

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

if (!arrow_str.IsEmpty() && !err_obj.IsEmpty() && err_obj->IsNativeError()) {
if (!arrow_str.IsEmpty() && !err_obj.IsEmpty() &&
(!handlingFatalError || err_obj->IsNativeError())) {
err_obj->SetPrivate(
env->context(),
env->arrow_message_private_symbol(),
arrow_str);
return;
}

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

AppendExceptionLine(env, er, message);
AppendExceptionLine(env, er, message, true);

Local<Value> trace_value;
Local<Value> arrow;
Expand Down
3 changes: 2 additions & 1 deletion src/node_internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ bool IsExceptionDecorated(Environment* env, v8::Local<v8::Value> er);

void AppendExceptionLine(Environment* env,
v8::Local<v8::Value> er,
v8::Local<v8::Message> message);
v8::Local<v8::Message> message,
bool handlingFatalError = false);

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

Expand Down
17 changes: 17 additions & 0 deletions test/message/vm_caught_custom_runtime_error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';
require('../common');
const vm = require('vm');

console.error('beginning');

// Regression test for https://github.com/nodejs/node/issues/7397:
// This should not print out anything to stderr due to the error being caught.
try {
vm.runInThisContext(`throw ({
name: 'MyCustomError',
message: 'This is a custom message'
})`, { filename: 'test.vm' });
} catch (e) {
}

console.error('end');
2 changes: 2 additions & 0 deletions test/message/vm_caught_custom_runtime_error.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
beginning
end

0 comments on commit 7af968a

Please sign in to comment.