Skip to content

Commit

Permalink
worker: use correct ctor for error serialization
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
addaleax committed Feb 8, 2019
1 parent 8b79c15 commit de9d5ff
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 1 deletion.
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++) {
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

0 comments on commit de9d5ff

Please sign in to comment.