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 };
2 changes: 1 addition & 1 deletion test/es-module/test-esm-loader-with-syntax-error.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe('ESM: loader with syntax error', { concurrency: !process.env.TEST_PARAL
path('print-error-message.js'),
]);

match(stderr, /SyntaxError \[Error\]:/);
match(stderr, /SyntaxError:/);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this previous behaviour was a result of always assigning [Symbol.toStringTag]: 'Error' on the deserialised error.

ok(!stderr.includes('Bad command or file name'));
notStrictEqual(code, 0);
});
Expand Down
34 changes: 34 additions & 0 deletions test/parallel/test-worker-error-serdes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const { isNativeError } = require('util/types');
const { Worker } = require('worker_threads');

const validateError = (error, ctor) => {
assert.strictEqual(error.constructor, ctor);
assert.strictEqual(Object.getPrototypeOf(error), ctor.prototype);
assert(isNativeError(error));
};

{
const w = new Worker('throw new Error()', { eval: true });
w.on('error', common.mustCall((error) => {
validateError(error, Error);
}));
}

{
const w = new Worker('throw new RangeError()', { eval: true });
w.on('error', common.mustCall((error) => {
validateError(error, RangeError);
}));
}

{
const w = new Worker('throw new Error(undefined, { cause: new TypeError() })', { eval: true });
w.on('error', common.mustCall((error) => {
validateError(error, Error);
assert.notStrictEqual(error.cause, undefined);
validateError(error.cause, TypeError);
}));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be worth expanding this test to see if it works with the new SuppressedError and AggregateError types.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did consider AggregateError, although neither of these are explicitly handled currently.

const errors = {
Error, TypeError, RangeError, URIError, SyntaxError, ReferenceError, EvalError,
};

At the moment, the logic traverses the prototype chain and ends up serialising these as Errors, so they end up with a prototype of Error.prototype. It doesn't really fall within the scope of this PR, but I can add a todo if desired?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added a TODO to error_serdes for the uncovered constructors.

}
Loading