When error handling affects control-flow invariants (dedupe keys, stale-session guards, correctness checks), make failures safe and reversible:
Example pattern (dedupe cleanup):
const dedupeKey = imageBlobDedupeKey(blob, data)
if (rememberRecentImageBlobPaste(refs, dedupeKey)) return
try {
const ok = await saveImageBuffer(...)
if (!ok) throw new Error('save failed')
} catch (e) {
// restore invariant so immediate retry can attach
forgetRecentImageBlobPaste(refs, dedupeKey)
throw e
}
Example pattern (guard integrity):
try {
const remote = await getSessionMessages(guardStoredId /* required routing */)
// decide based on remote
} catch (e) {
// safe default: block + refresh rather than submit with stale context
await refreshSessionContext()
return
}