While working on the UI process, I need to capture custom Errors thrown in a Webworker using comlink.
The custom Error is an extended Error type, so comlink serializes/deserializes it as a regular Error object, not as a custom Error.
Serialization:
if (value instanceof Error) {
serialized = {
isError: true,
value: {
message: value.message,
name: value.name,
stack: value.stack,
},
};
}
Link to Source Code - Line 248
Deserialization:
if (serialized.isError) {
throw Object.assign(
new Error(serialized.value.message),
serialized.value
);
}
Link to Source Code - Line 265
I attempted to customize the throwTransferHandler to throw custom Errors, but was unsuccessful. This is because throwMarker is a unique Symbol and not exported.
canHandle: (value): value is ThrownValue =>
isObject(value) && throwMarker in value,