import * as Either from 'fp-ts/lib/Either' ; import { errors as EsErrors } from '@elastic/elasticsearch' ; const retryResponseStatuses = [ 503 , 401 , 403 , 408 , 410 , ] ; export interface RetryableEsClientError { type : 'retryable_es_client_error' ; message : string ; error ? : Error ; } export const catchRetryableEsClientErrors = ( e ) : Either . Either < RetryableEsClientError , never > => { if ( e instanceof EsErrors . NoLivingConnectionsError || e instanceof EsErrors . ConnectionError || e instanceof EsErrors . TimeoutError || ( e instanceof EsErrors . ResponseError && ( retryResponseStatuses . includes ( e ?. statusCode ! ) || e ?. body ?. error ?. type === 'snapshot_in_progress_exception' ) ) ) { return Either . left ( { type : 'retryable_es_client_error' as const , message : e ?. message , error : e , } ) ; } else { throw e ; } } ;