export interface ESErrorCausedBy { type ? : string ; reason ? : string ; caused_by ? : ESErrorCausedBy ; } export interface ESError { root_cause ? : ESErrorCausedBy [ ] ; caused_by ? : ESErrorCausedBy ; } export interface ESErrorBody { error ? : ESError ; status ? : number ; } export interface ESErrorMeta { body ? : ESErrorBody ; statusCode ? : number ; } export interface ElasticsearchResponseError { name ? : string ; meta ? : ESErrorMeta ; } function extractCausedByChain ( causedBy = { } , accumulator = [ ] ) { const { reason , caused_by : innerCausedBy } = causedBy ; if ( reason && ! accumulator . includes ( reason ) ) { accumulator . push ( reason ) ; } if ( innerCausedBy ) { return extractCausedByChain ( innerCausedBy , accumulator ) ; } return accumulator ; } param err return export function identifyEsError ( err ) { if ( ! err . meta ) { return [ ] ; } const { meta : { body : response } , } = err ; if ( response ) { const { error } = response ; if ( error ) { const { root_cause : rootCause = [ ] , caused_by : causedBy } = error ; return [ ... extractCausedByChain ( causedBy ) , ... rootCause . reduce ( ( acc , innerRootCause ) => extractCausedByChain ( innerRootCause , acc ) , [ ] ) , ] ; } } return [ ] ; } export function isEsCannotExecuteScriptError ( err ) { return identifyEsError ( err ) . includes ( 'cannot execute [inline] scripts' ) ; }