<commit_before>import is from '@sindresorhus/is';
import pAll from 'p-all';
import parseLinkHeader from 'parse-link-header';
import {
  PLATFORM_BAD_CREDENTIALS,
  PLATFORM_INTEGRATION_UNAUTHORIZED,
  PLATFORM_RATE_LIMIT_EXCEEDED,
  REPOSITORY_CHANGED,
} from '../../constants/error-messages';
import { PLATFORM_TYPE_GITHUB } from '../../constants/platforms';
import { logger } from '../../logger';
import { ExternalHostError } from '../../types/errors/external-host-error';
import { maskToken } from '../mask';
import { GotLegacyError } from './legacy';
import { Http, HttpPostOptions, HttpResponse, InternalHttpOptions } from '.';

let baseUrl = 'https://api.github.com/';
export const setBaseUrl = (url: string): void => {
  baseUrl = url;
};

interface GithubInternalOptions extends InternalHttpOptions {
  body?: string;
}

export interface GithubHttpOptions extends InternalHttpOptions {
  paginate?: boolean | string;
  pageLimit?: number;
  token?: string;
}

interface GithubGraphqlResponse<T = unknown> {
  data?: {
    repository?: T;
  };
  errors?: { message: string; locations: unknown }[];
}

function handleGotError(
  err: GotLegacyError,
  url: string | URL,
  opts: GithubHttpOptions
): never {
  const path = url.toString();
  let message = err.message || '';
  if (is.plainObject(err.response?.body) && 'message' in err.response.body) {
    message = String(err.response.body.message);
  }
  if (
    err.name === 'RequestError' &&
    (err.code === 'ENOTFOUND' ||
      err.code === 'ETIMEDOUT' ||
      err.code === 'EAI_AGAIN')
  ) {
    logger.debug({ err }, 'GitHub failure: RequestError');
    throw new ExternalHostError(err, PLATFORM_TYPE_GITHUB);
  }
  if (err.name === 'ParseError') {
    logger.debug({ err }, '');
    throw new ExternalHostError(err, PLATFORM_TYPE_GITHUB);
  }
  if (err.statusCode >= 500 && err.statusCode < 600) {
    logger.debug({ err }, 'GitHub failure: 5xx');
    throw new ExternalHostError(err, PLATFORM_TYPE_GITHUB);<commit_msg>fix(http): Throw ExternalHostError on ECONNRESET (#9000)<commit_after>import is from '@sindresorhus/is';
import pAll from 'p-all';
import parseLinkHeader from 'parse-link-header';
import {
  PLATFORM_BAD_CREDENTIALS,
  PLATFORM_INTEGRATION_UNAUTHORIZED,
  PLATFORM_RATE_LIMIT_EXCEEDED,
  REPOSITORY_CHANGED,
} from '../../constants/error-messages';
import { PLATFORM_TYPE_GITHUB } from '../../constants/platforms';
import { logger } from '../../logger';
import { ExternalHostError } from '../../types/errors/external-host-error';
import { maskToken } from '../mask';
import { GotLegacyError } from './legacy';
import { Http, HttpPostOptions, HttpResponse, InternalHttpOptions } from '.';

let baseUrl = 'https://api.github.com/';
export const setBaseUrl = (url: string): void => {
  baseUrl = url;
};

interface GithubInternalOptions extends InternalHttpOptions {
  body?: string;
}

export interface GithubHttpOptions extends InternalHttpOptions {
  paginate?: boolean | string;
  pageLimit?: number;
  token?: string;
}

interface GithubGraphqlResponse<T = unknown> {
  data?: {
    repository?: T;
  };
  errors?: { message: string; locations: unknown }[];
}

function handleGotError(
  err: GotLegacyError,
  url: string | URL,
  opts: GithubHttpOptions
): never {
  const path = url.toString();
  let message = err.message || '';
  if (is.plainObject(err.response?.body) && 'message' in err.response.body) {
    message = String(err.response.body.message);
  }
  if (
    err.name === 'RequestError' &&
    (err.code === 'ENOTFOUND' ||
      err.code === 'ETIMEDOUT' ||
      err.code === 'EAI_AGAIN' ||
      err.code === 'ECONNRESET')
  ) {
    logger.debug({ err }, 'GitHub failure: RequestError');
    throw new ExternalHostError(err, PLATFORM_TYPE_GITHUB);
  }
  if (err.name === 'ParseError') {
    logger.debug({ err }, '');
    throw new ExternalHostError(err, PLATFORM_TYPE_GITHUB);
  }
  if (err.statusCode >= 500 && err.statusCode < 600) {
    logger.debug({ err }, 'GitHub failure: 5xx');
    throw new ExternalHostError(err, PLATFORM_TYPE_GITHUB);
{'fraction_non_alphanumeric': 0.15153010858835142, 'fraction_numerical': 0.004442250740375123, 'mean_word_length': 4.123893805309734, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6577004', 'n_tokens_mistral': 1342, 'n_tokens_neox': 1332, 'n_words': 304}