Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions lib/internal/error_serdes.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const {
EvalError,
FunctionPrototypeCall,
ObjectAssign,
ObjectCreate,
ObjectDefineProperties,
ObjectDefineProperty,
ObjectGetOwnPropertyDescriptor,
ObjectGetOwnPropertyNames,
Expand All @@ -15,11 +15,12 @@ const {
ObjectPrototypeToString,
RangeError,
ReferenceError,
ReflectConstruct,
ReflectDeleteProperty,
SafeSet,
StringFromCharCode,
StringPrototypeSubstring,
SymbolFor,
SymbolToStringTag,
SyntaxError,
TypeError,
TypedArrayPrototypeGetBuffer,
Expand All @@ -28,6 +29,8 @@ const {
URIError,
} = primordials;

const assert = require('internal/assert');

const { Buffer } = require('buffer');
const { inspect: { custom: customInspectSymbol } } = require('util');

Expand All @@ -40,6 +43,7 @@ const kCircularReference = 5;

const kSymbolStringLength = 'Symbol('.length;

// TODO: implement specific logic for AggregateError/SuppressedError
const errors = {
Error, TypeError, RangeError, URIError, SyntaxError, ReferenceError, EvalError,
};
Expand Down Expand Up @@ -166,16 +170,17 @@ function deserializeError(error) {
switch (error[0]) {
case kSerializedError: {
const { constructor, properties } = deserialize(error.subarray(1));
const ctor = errors[constructor];
ObjectDefineProperty(properties, SymbolToStringTag, {
__proto__: null,
value: { __proto__: null, value: 'Error', configurable: true },
enumerable: true,
});
assert(errorConstructorNames.has(constructor), 'Invalid constructor');
if ('cause' in properties && 'value' in properties.cause) {
properties.cause.value = deserializeError(properties.cause.value);
}
return ObjectCreate(ctor.prototype, properties);
// Invoke the Error constructor to gain an object with an [[ErrorData]] internal slot
const ret = ReflectConstruct(Error, [], errors[constructor]);
// Delete any properties defined by the Error constructor before assigning from source
ArrayPrototypeForEach(ObjectGetOwnPropertyNames(ret), (key) => {
ReflectDeleteProperty(ret, key);
});
return ObjectDefineProperties(ret, properties);
}
case kSerializedObject:
return deserialize(error.subarray(1));
Expand All @@ -196,7 +201,7 @@ function deserializeError(error) {
[customInspectSymbol]: () => '[Circular object]',
};
}
require('assert').fail('This should not happen');
assert.fail('Unknown serializer flag');
}

module.exports = { serializeError, deserializeError };
36 changes: 36 additions & 0 deletions test/parallel/test-worker-error-serdes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const { isNativeError } = require('util/types');
const { Worker } = require('worker_threads');

{
const w = new Worker('throw new Error()', { eval: true });
w.on('error', common.mustCall((error) => {
assert(isNativeError(error));
assert.strictEqual(error.constructor, Error);
assert.strictEqual(Object.getPrototypeOf(error), Error.prototype);
}));
}

{
const w = new Worker('throw new RangeError()', { eval: true });
w.on('error', common.mustCall((error) => {
assert(isNativeError(error));
assert.strictEqual(error.constructor, RangeError);
assert.strictEqual(Object.getPrototypeOf(error), RangeError.prototype);
}));
}

{
const w = new Worker('throw new Error(undefined, { cause: new TypeError() })', { eval: true });
w.on('error', common.mustCall((error) => {
assert(isNativeError(error));
assert.strictEqual(error.constructor, Error);
assert.strictEqual(Object.getPrototypeOf(error), Error.prototype);

assert(isNativeError(error.cause));
assert.strictEqual(error.cause.constructor, TypeError);
assert.strictEqual(Object.getPrototypeOf(error.cause), TypeError.prototype);
}));
}
Loading