Skip to content

Commit de9d5ff

Browse files
committed
worker: use correct ctor for error serialization
When serializing errors, use the error constructor that is closest to the object itself in the prototype chain. The previous practice of walking downwards meant that `Error` would usually be the first constructor that is used, even when a more specific one would be available/appropriate, because it is the base class of the other common error types. PR-URL: #25951 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Minwoo Jung <minwoo@nodesource.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
1 parent 8b79c15 commit de9d5ff

File tree

3 files changed

+3
-1
lines changed

3 files changed

+3
-1
lines changed

lib/internal/error-serdes.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ function serializeError(error) {
8686
if (typeof error === 'object' &&
8787
ObjectPrototypeToString(error) === '[object Error]') {
8888
const constructors = GetConstructors(error);
89-
for (var i = constructors.length - 1; i >= 0; i--) {
89+
for (var i = 0; i < constructors.length; i++) {
9090
const name = GetName(constructors[i]);
9191
if (errorConstructorNames.has(name)) {
9292
try { error.stack; } catch {}

test/parallel/test-worker-syntax-error-file.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ if (!process.env.HAS_STARTED_WORKER) {
1010
const w = new Worker(fixtures.path('syntax', 'bad_syntax.js'));
1111
w.on('message', common.mustNotCall());
1212
w.on('error', common.mustCall((err) => {
13+
assert.strictEqual(err.constructor, SyntaxError);
1314
assert(/SyntaxError/.test(err));
1415
}));
1516
} else {

test/parallel/test-worker-syntax-error.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ if (!process.env.HAS_STARTED_WORKER) {
99
const w = new Worker('abc)', { eval: true });
1010
w.on('message', common.mustNotCall());
1111
w.on('error', common.mustCall((err) => {
12+
assert.strictEqual(err.constructor, SyntaxError);
1213
assert(/SyntaxError/.test(err));
1314
}));
1415
} else {

0 commit comments

Comments
 (0)