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

100% Statements 8/8
100% Branches 12/12
100% Functions 1/1
100% Lines 8/8

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                    22x             22x 8x       14x 7x       7x       1x     6x    
/**
 * Extract a network error code from an error object.
 *
 * Returns error codes like 'ECONNREFUSED', 'ETIMEDOUT', etc., or
 * the error name for libp2p errors, or 'UNKNOWN' for unrecognized errors.
 *
 * @param error - The error to extract the code from.
 * @returns The error code string.
 */
export function getNetworkErrorCode(error: unknown): string {
  const anyError = error as {
    code?: string;
    name?: string;
    message?: string;
  };
 
  // Node.js network error codes (ECONNREFUSED, ETIMEDOUT, etc.)
  if (typeof anyError?.code === 'string' && anyError.code.length > 0) {
    return anyError.code;
  }
 
  // libp2p errors and other named errors
  if (typeof anyError?.name === 'string' && anyError.name.length > 0) {
    return anyError.name;
  }
 
  // Check message for relay reservation errors
  if (
    typeof anyError?.message === 'string' &&
    anyError.message.includes('NO_RESERVATION')
  ) {
    return 'NO_RESERVATION';
  }
 
  return 'UNKNOWN';
}