Skip to content

Commit

Permalink
worker: serialize errors if stack getter throws
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
Trott and addaleax committed Feb 20, 2019
1 parent 5c9b37b commit 6e7f287
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/internal/error-serdes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
22 changes: 22 additions & 0 deletions test/parallel/test-worker-error-stack-getter-throws.js
Original file line number Diff line number Diff line change
@@ -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');
}));

0 comments on commit 6e7f287

Please sign in to comment.