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 <anna@addaleax.net>
PR-URL: #26145
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
  • Loading branch information
2 people authored and rvagg committed Feb 28, 2019
1 parent b1e739d commit 7e70233
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 7e70233

Please sign in to comment.