From 6e7f287f2abcb1333bb5cb6eda9543ff07401cb0 Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Fri, 15 Feb 2019 22:43:58 -0800 Subject: [PATCH] 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 --- lib/internal/error-serdes.js | 6 +++-- .../test-worker-error-stack-getter-throws.js | 22 +++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 test/parallel/test-worker-error-stack-getter-throws.js diff --git a/lib/internal/error-serdes.js b/lib/internal/error-serdes.js index 7b4b416b80c670..1fed04f0a9864f 100644 --- a/lib/internal/error-serdes.js +++ b/lib/internal/error-serdes.js @@ -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) diff --git a/test/parallel/test-worker-error-stack-getter-throws.js b/test/parallel/test-worker-error-stack-getter-throws.js new file mode 100644 index 00000000000000..108fa3f5143d18 --- /dev/null +++ b/test/parallel/test-worker-error-stack-getter-throws.js @@ -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'); +}));