Skip to content
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
2 changes: 1 addition & 1 deletion lib/internal/error-serdes.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ function serializeError(error) {
if (typeof error === 'object' &&
ObjectPrototypeToString(error) === '[object Error]') {
const constructors = GetConstructors(error);
for (var i = constructors.length - 1; i >= 0; i--) {
for (var i = 0; i < constructors.length; i++) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe for (const name of constructors) ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gengjiawen The idea here is that this code is supposed to be as robust against monkey-patching as possible, because we do not want to generate more exceptions while attempting to handle another exception – with for (const … of …), it’s possible to override Array.prototype[Symbol.iterator]() and give false results or throw another exception.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the explain :)

const name = GetName(constructors[i]);
if (errorConstructorNames.has(name)) {
try { error.stack; } catch {}
Expand Down
1 change: 1 addition & 0 deletions test/parallel/test-worker-syntax-error-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ if (!process.env.HAS_STARTED_WORKER) {
const w = new Worker(fixtures.path('syntax', 'bad_syntax.js'));
w.on('message', common.mustNotCall());
w.on('error', common.mustCall((err) => {
assert.strictEqual(err.constructor, SyntaxError);
assert(/SyntaxError/.test(err));
}));
} else {
Expand Down
1 change: 1 addition & 0 deletions test/parallel/test-worker-syntax-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ if (!process.env.HAS_STARTED_WORKER) {
const w = new Worker('abc)', { eval: true });
w.on('message', common.mustNotCall());
w.on('error', common.mustCall((err) => {
assert.strictEqual(err.constructor, SyntaxError);
assert(/SyntaxError/.test(err));
}));
} else {
Expand Down