import Boom from '@hapi/boom' ; import { extractErrorMessage , MLHttpFetchError , MLResponseError , EsErrorBody } from './index' ; describe ( 'ML - error message utils' , ( ) => { describe ( 'extractErrorMessage' , ( ) => { test ( 'returns just the error message' , ( ) => { const testMsg = 'Saved object [index-pattern/indexpattern] not found' ; const badError = { } as any ; expect ( extractErrorMessage ( badError ) ) . toBe ( '' ) ; const esErrorMsg = { error : { root_cause : [ { type : 'type' , reason : 'reason' , } , ] , type : 'type' , reason : testMsg , } , status : 404 , } ; expect ( extractErrorMessage ( esErrorMsg ) ) . toBe ( testMsg ) ; const stringMessage = testMsg ; expect ( extractErrorMessage ( stringMessage ) ) . toBe ( testMsg ) ; const bodyWithoutAttributes = { name : 'name' , req : { } as Request , request : { } as Request , message : 'Something else' , body : { statusCode : 404 , error : 'error' , message : testMsg , } , } ; expect ( extractErrorMessage ( bodyWithoutAttributes ) ) . toBe ( testMsg ) ; const bodyWithAttributes = { name : 'name' , req : { } as Request , request : { } as Request , message : 'Something else' , body : { statusCode : 404 , error : 'error' , message : 'Something else' , attributes : { body : { status : 404 , error : { reason : testMsg , type : 'type' , root_cause : [ { type : 'type' , reason : 'reason' } ] , } , } , } , } , } ; expect ( extractErrorMessage ( bodyWithAttributes ) ) . toBe ( testMsg ) ; const boomError = { message : '' , typeof : Boom . Boom . constructor , reformat : ( ) => '' , name : '' , data : [ ] , isBoom : true , isServer : false , output : { statusCode : 404 , payload : { statusCode : 404 , error : testMsg , message : testMsg , } , headers : { } , } , } ; expect ( extractErrorMessage ( boomError ) ) . toBe ( testMsg ) ; } ) ; } ) ; } ) ;