import { ElasticsearchError , ElasticsearchErrorCausedByObject } from './types' ; const getEsCause = ( obj = { } , causes = [ ] ) : string [ ] => { const updated = [ ... causes ] ; if ( obj . caused_by ) { if ( obj . caused_by ?. reason ) { updated . push ( obj . caused_by ?. reason ) ; } return getEsCause ( obj . caused_by , updated ) ; } if ( obj . failed_shards && obj . failed_shards . length ) { for ( const failure of obj . failed_shards ) { if ( failure && failure . reason ) { updated . push ( ... getEsCause ( failure . reason ) ) ; } } } return updated . filter ( Boolean ) ; } ; export const getEsErrorMessage = ( error ) => { let message = error ?. message ; const apiError = error ?. error ?. meta ?. body ?. error ?? error ?. meta ?. body ?. error ; if ( apiError ) { message += `, caused by: "${ getEsCause ( apiError as ElasticsearchErrorCausedByObject ) }"` ; } return message ; } ;