Skip to content

src: don't print garbage errors #4112

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1512,8 +1512,10 @@ static void ReportException(Environment* env,
name.IsEmpty() ||
name->IsUndefined()) {
// Not an error object. Just print as-is.
node::Utf8Value message(env->isolate(), er);
PrintErrorString("%s\n", *message);
String::Utf8Value message(er);

PrintErrorString("%s\n", *message ? *message :
"<toString() threw exception>");
} else {
node::Utf8Value name_string(env->isolate(), name);
node::Utf8Value message_string(env->isolate(), message);
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/throws_error7.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
throw {
toString: function() {
throw this;
}
};
9 changes: 6 additions & 3 deletions test/parallel/test-error-reporting.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ function errExec(script, callback) {

// Count the tests
exits++;

console.log('.');
});
}

Expand Down Expand Up @@ -64,6 +62,11 @@ errExec('throws_error6.js', function(err, stdout, stderr) {
assert.ok(/SyntaxError/.test(stderr));
});

// Object that throws in toString() doesn't print garbage
errExec('throws_error7.js', function(err, stdout, stderr) {
assert.ok(/<toString\(\) threw exception/.test(stderr));
});

process.on('exit', function() {
assert.equal(6, exits);
assert.equal(7, exits);
});