Skip to content

Commit 7e70233

Browse files
Trottaddaleax
authored andcommittedFeb 28, 2019
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 <[email protected]> PR-URL: #26145 Reviewed-By: Gus Caplan <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
1 parent b1e739d commit 7e70233

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed
 

‎lib/internal/error-serdes.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ function TryGetAllProperties(object, target = object) {
3636
Assign(all, TryGetAllProperties(GetPrototypeOf(object), target));
3737
const keys = GetOwnPropertyNames(object);
3838
ForEach(keys, (key) => {
39-
const descriptor = GetOwnPropertyDescriptor(object, key);
39+
let descriptor;
40+
try {
41+
descriptor = GetOwnPropertyDescriptor(object, key);
42+
} catch { return; }
4043
const getter = descriptor.get;
4144
if (getter && key !== '__proto__') {
4245
try {
@@ -89,7 +92,6 @@ function serializeError(error) {
8992
for (var i = 0; i < constructors.length; i++) {
9093
const name = GetName(constructors[i]);
9194
if (errorConstructorNames.has(name)) {
92-
try { error.stack; } catch {}
9395
const serialized = serialize({
9496
constructor: name,
9597
properties: TryGetAllProperties(error)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const { Worker } = require('worker_threads');
5+
6+
const w = new Worker(
7+
`const fn = (err) => {
8+
if (err.message === 'fhqwhgads')
9+
throw new Error('come on');
10+
return 'This is my custom stack trace!';
11+
};
12+
Error.prepareStackTrace = fn;
13+
throw new Error('fhqwhgads');
14+
`,
15+
{ eval: true }
16+
);
17+
w.on('message', common.mustNotCall());
18+
w.on('error', common.mustCall((err) => {
19+
assert.strictEqual(err.stack, undefined);
20+
assert.strictEqual(err.message, 'fhqwhgads');
21+
assert.strictEqual(err.name, 'Error');
22+
}));

0 commit comments

Comments
 (0)
Please sign in to comment.