import { IBKRConnection } from '../connection' ; import isEmpty from 'lodash/isEmpty' ; import { verbose } from '../log' ; import { EventName } from '@stoqey/ib' ; param targetErrors param catchError returns important export function handleEventfulError ( reqId , targetErrors , catchError ) { if ( isEmpty ( targetErrors ) ) { return ( ) => { } ; } targetErrors = targetErrors . map ( ( er ) => ( er || '' ) . toLowerCase ( ) ) ; const ib = IBKRConnection . Instance . getIBKR ( ) ; const handleError = ( error , errorData ) => { const reqIdFromError = errorData && errorData . id ; if ( reqId && reqIdFromError === reqId ) { verbose ( 'handleEventfulError > handleError.reqIdFromError' , reqId ) ; catchError ( ) ; } const errorMessage = ( ( error && error . message ) || '' ) . toLowerCase ( ) ; const isError = targetErrors . some ( ( tError ) => errorMessage . includes ( tError ) ) ; if ( isError ) { verbose ( 'handleEventfulError > handleError.errorMessage' , errorMessage ) ; catchError ( ) ; } } ; ib . on ( EventName . error , handleError ) ; return ( ) => { ib . off ( EventName . error , handleError ) ; } ; }