Error resiliency on duplicating parts that don't exist anymore

This commit is contained in:
Enrico Ros
2025-04-24 15:32:51 -07:00
parent b0f0e35170
commit fb7dfdf341
+12 -1
View File
@@ -440,7 +440,8 @@ function _create_Sentinel_Part(): _SentinelPart {
}
function _duplicate_Part<TPart extends (DMessageContentFragment | DMessageAttachmentFragment | DMessageVoidFragment)['part']>(part: TPart): TPart {
switch (part.pt) {
const pt = part.pt;
switch (pt) {
case 'doc':
const newDocVersion = Number(part.version ?? 1); // we don't increase the version on duplication (not sure we should?)
return _create_Doc_Part(part.vdt, _duplicate_InlineData(part.data), part.ref, part.l1Title, newDocVersion, part.meta ? { ...part.meta } : undefined) as TPart;
@@ -481,6 +482,16 @@ function _duplicate_Part<TPart extends (DMessageContentFragment | DMessageAttach
case '_pt_sentinel':
return _create_Sentinel_Part() as TPart;
default:
const _exhaustiveCheck: never = pt;
// console.warn('[DEV] _duplicate_Part: Unknown part type, will duplicate as Error', { part });
// return _create_Error_Part(`Unknown part type '${(part as any)?.pt || '(undefined)'}'`) as TPart;
// unexpected case: if we are here, the best to do is probably to return a clone of the part, as returning
// nothing would corrupt the Fragment
return structuredClone(part) as TPart; // fallback to structured clone for unknown parts
}
}