All files / kernel-errors/src/utils isTerminalSendError.ts

100% Statements 2/2
100% Branches 2/2
100% Functions 1/1
100% Lines 2/2

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31                          157x                             19x    
import { IntentionalCloseError } from '../errors/IntentionalCloseError.ts';
import { NetworkStoppedError } from '../errors/NetworkStoppedError.ts';
import { PeerRestartedError } from '../errors/PeerRestartedError.ts';
 
/**
 * Names of the sentinel errors that mean retransmit/retry should abort —
 * derived from the classes themselves so adding a new terminal class without
 * registering it would be an obvious omission rather than a silent drift.
 *
 * Detection uses `name` (not `instanceof`) because errors cross the
 * platform-services RPC boundary as serialized JSON-RPC error envelopes
 * that don't preserve class identity. The `name` field is preserved.
 */
const TERMINAL_NAMES: ReadonlySet<string> = new Set([
  PeerRestartedError.name,
  IntentionalCloseError.name,
  NetworkStoppedError.name,
]);
 
/**
 * Whether a thrown send-side error is a terminal verdict from the transport
 * (peer restart, intentional close, network stopped). Recipients should
 * abort retransmit and reject pending state instead of retrying.
 *
 * @param error - The error thrown by `sendRemoteMessage`.
 * @returns True if the error is a terminal verdict.
 */
export function isTerminalSendError(error: unknown): boolean {
  return error instanceof Error && TERMINAL_NAMES.has(error.name);
}