import { errors } from '@elastic/elasticsearch' ; import { inspect } from 'util' ; export class WrappedElasticsearchClientError extends Error { originalError : errors . ElasticsearchClientError ; constructor ( originalError ) { super ( originalError . message ) ; const stack = this . stack ; this . originalError = originalError ; if ( originalError instanceof errors . ResponseError ) { this . stack = { valueOf ( ) { const value = stack ?. valueOf ( ) ?? '' ; return value ; } , toString ( ) { const value = stack ?. toString ( ) + `\nResponse: ${ inspect ( originalError . meta . body , { depth : null } ) }\n` ; return value ; } , } ; } } } export function unwrapEsResponse < T extends Promise < { body : any ; } > > ( responsePromise ) { return responsePromise . then ( ( res ) => res . body ) . catch ( ( err ) => { throw new WrappedElasticsearchClientError ( err ) ; } ) ; }