Skip to content

Commit 508a2e7

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 469cdac commit 508a2e7

File tree

3 files changed

+3
-1
lines changed

3 files changed

+3
-1
lines changed

lib/internal/error-serdes.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ function serializeError(error) {
8080
if (typeof error === 'object' &&
8181
ObjectPrototypeToString(error) === '[object Error]') {
8282
const constructors = GetConstructors(error);
83-
for (var i = constructors.length - 1; i >= 0; i--) {
83+
for (var i = 0; i < constructors.length; i++) {
8484
const name = GetName(constructors[i]);
8585
if (errorConstructorNames.has(name)) {
8686
try { error.stack; } catch {}

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

+1
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

+1
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)