From 508a2e7f0f7399a6de0bab277f0e421b9de2b70b Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Tue, 5 Feb 2019 21:47:08 +0100 Subject: [PATCH] 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: https://github.com/nodejs/node/pull/25951 Reviewed-By: James M Snell Reviewed-By: Gus Caplan Reviewed-By: Benjamin Gruenbaum Reviewed-By: Minwoo Jung Reviewed-By: Colin Ihrig --- lib/internal/error-serdes.js | 2 +- test/parallel/test-worker-syntax-error-file.js | 1 + test/parallel/test-worker-syntax-error.js | 1 + 3 files changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/internal/error-serdes.js b/lib/internal/error-serdes.js index 9da1a864171607..d6e15ac77397cf 100644 --- a/lib/internal/error-serdes.js +++ b/lib/internal/error-serdes.js @@ -80,7 +80,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++) { const name = GetName(constructors[i]); if (errorConstructorNames.has(name)) { try { error.stack; } catch {} diff --git a/test/parallel/test-worker-syntax-error-file.js b/test/parallel/test-worker-syntax-error-file.js index 87ed6c3c92dc64..ca42c174808500 100644 --- a/test/parallel/test-worker-syntax-error-file.js +++ b/test/parallel/test-worker-syntax-error-file.js @@ -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 { diff --git a/test/parallel/test-worker-syntax-error.js b/test/parallel/test-worker-syntax-error.js index 86c20ab29d7bf1..5c91eb2d251204 100644 --- a/test/parallel/test-worker-syntax-error.js +++ b/test/parallel/test-worker-syntax-error.js @@ -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 {