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

100% Statements 13/13
100% Branches 20/20
100% Functions 1/1
100% Lines 13/13

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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57                        33x           33x 1x             32x 33x                 7x       25x 33x       7x       18x 33x 5x     13x    
import { MuxerClosedError } from '@libp2p/interface';
 
/**
 * Decide if an error is retryable for reconnects
 *
 * @param error - The error to check if it is retryable.
 * @returns True if the error is retryable, false otherwise.
 */
export function isRetryableNetworkError(error: unknown): boolean {
  // Network errors from Node.js (ECONNRESET, ETIMEDOUT, etc.)
  // libp2p errors (Dial*, Transport*, etc.)
  // WebRTC/SCTP errors
  const anyError = error as {
    code?: string;
    name?: string;
    message?: string;
  };
 
  if (error instanceof MuxerClosedError) {
    return true;
  }
 
  // Node.js network error codes
  // Note: ENOTFOUND (DNS lookup failed) is included to allow permanent failure
  // detection to work - after multiple consecutive ENOTFOUND errors, the peer
  // will be marked as permanently failed rather than giving up immediately.
  const code = anyError?.code;
  if (
    code === 'ECONNRESET' ||
    code === 'ETIMEDOUT' ||
    code === 'EPIPE' ||
    code === 'ECONNREFUSED' ||
    code === 'EHOSTUNREACH' ||
    code === 'ENETUNREACH' ||
    code === 'ENOTFOUND'
  ) {
    return true;
  }
 
  // libp2p dial/transport errors
  const name = anyError?.name;
  if (
    typeof name === 'string' &&
    (name.includes('Dial') || name.includes('Transport'))
  ) {
    return true;
  }
 
  // Relay reservation errors - these are temporary and should be retryable
  const message = anyError?.message;
  if (typeof message === 'string' && message.includes('NO_RESERVATION')) {
    return true;
  }
 
  return false; // default to non-retryable for unknown errors
}