worker: serialize errors if stack getter throws

Current code that is intended to handle the stack getter throwing is
untested. Add a test and adjust code to function as expected.

Co-authored-by: Anna Henningsen <anna@addaleax.net>
PR-URL: https://github.com/nodejs/node/pull/26145
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
This commit is contained in:
Rich Trott 2019-02-15 22:43:58 -08:00
parent 47c784203c
commit 79a3348d14
2 changed files with 26 additions and 2 deletions

View File

@ -36,7 +36,10 @@ function TryGetAllProperties(object, target = object) {
Assign(all, TryGetAllProperties(GetPrototypeOf(object), target));
const keys = GetOwnPropertyNames(object);
ForEach(keys, (key) => {
const descriptor = GetOwnPropertyDescriptor(object, key);
let descriptor;
try {
descriptor = GetOwnPropertyDescriptor(object, key);
} catch { return; }
const getter = descriptor.get;
if (getter && key !== '__proto__') {
try {
@ -89,7 +92,6 @@ function serializeError(error) {
for (var i = 0; i < constructors.length; i++) {
const name = GetName(constructors[i]);
if (errorConstructorNames.has(name)) {
try { error.stack; } catch {}
const serialized = serialize({
constructor: name,
properties: TryGetAllProperties(error)

View File

@ -0,0 +1,22 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const { Worker } = require('worker_threads');
const w = new Worker(
`const fn = (err) => {
if (err.message === 'fhqwhgads')
throw new Error('come on');
return 'This is my custom stack trace!';
};
Error.prepareStackTrace = fn;
throw new Error('fhqwhgads');
`,
{ eval: true }
);
w.on('message', common.mustNotCall());
w.on('error', common.mustCall((err) => {
assert.strictEqual(err.stack, undefined);
assert.strictEqual(err.message, 'fhqwhgads');
assert.strictEqual(err.name, 'Error');
}));