import { ConnectionError , RequestTimeoutError , ServiceBusyError , UnrecognizedError , } from '@resolve-js/eventstore-base' ; const checkFormalError = ( error , value ) : boolean => error . name === value || error . code === value ; const checkFuzzyError = ( error , value ) : boolean => value . test ( error . message ) || value . test ( error . stack ) ; export const isConnectionTerminatedError = ( error ) : boolean => { return ( error != null && ( checkFuzzyError ( error , /Connection terminated/i ) || checkFuzzyError ( error , /terminating connection due to serverless scale event timeout/i ) || checkFuzzyError ( error , /terminating connection due to administrator command/i ) || checkFuzzyError ( error , /Client has encountered a connection error and is not queryable/i ) || checkFormalError ( error , 'ECONNRESET' ) || checkFormalError ( error , '08000' ) || checkFormalError ( error , '08003' ) || checkFormalError ( error , '08006' ) ) ) ; } ; export const isTimeoutError = ( error ) : boolean => { return ( error != null && ( checkFuzzyError ( error , /canceling statement due to statement timeout/i ) || checkFuzzyError ( error , /Query read timeout/i ) || checkFuzzyError ( error , /timeout expired/i ) || checkFormalError ( error , 'ETIMEDOUT' ) ) ) ; } ; export const isServiceBusyError = ( error ) : boolean => { return ( error != null && ( checkFuzzyError ( error , /too many clients already/i ) || checkFuzzyError ( error , /Connection rate is too high, please reduce connection rate/i ) || checkFormalError ( error , '55P03' ) || checkFuzzyError ( error , /could not obtain lock on relation/ ) || checkFuzzyError ( error , /getaddrinfo/ ) || checkFuzzyError ( error , /SQLState: 08001/ ) || checkFuzzyError ( error , /remaining connection slots are reserved/i ) ) ) ; } ; function extendErrorStack ( mainError , origError ) { if ( origError . stack ) { if ( mainError . stack ) { mainError . stack += '\n' ; mainError . stack += origError . stack ; } else { mainError . stack = origError . stack ; } } } export const makeKnownError = ( error ) : Error | null => { if ( isServiceBusyError ( error ) || isConnectionTerminatedError ( error ) ) { const busyError = new ServiceBusyError ( error . message ) ; extendErrorStack ( busyError , error ) ; return busyError ; } else if ( isTimeoutError ( error ) ) { const timeoutError = new RequestTimeoutError ( error . message ) ; extendErrorStack ( timeoutError , error ) ; return timeoutError ; } else { return null ; } } ; export const makeConnectionError = ( error ) : Error => { const knownError = makeKnownError ( error ) ; if ( knownError !== null ) { return knownError ; } else { const connectionError = new ConnectionError ( error . message ) ; extendErrorStack ( connectionError , error ) ; return connectionError ; } } ; export const makeUnrecognizedError = ( error ) : Error => { if ( error ) { const unrecognizedError = new UnrecognizedError ( error . message ) ; unrecognizedError . code = error . code ; extendErrorStack ( unrecognizedError , error ) ; return unrecognizedError ; } else { return new UnrecognizedError ( 'unrecognized error' ) ; } } ;