import Boom from '@hapi/boom' ; import { i18n } from '@kbn/i18n' ; import { ResponseError , CustomHttpResponseOptions } from 'src/core/server' ; import { CommonResponseStatusSchema , TransformIdsSchema } from '../../../common/api_schemas/common' ; import { DeleteTransformsResponseSchema } from '../../../common/api_schemas/delete_transforms' ; const REQUEST_TIMEOUT = 'RequestTimeout' ; export function isRequestTimeout ( error ) { return error . displayName === REQUEST_TIMEOUT ; } interface Params { results : CommonResponseStatusSchema | DeleteTransformsResponseSchema ; id : string ; items : TransformIdsSchema ; action : string ; } export function fillResultsWithTimeouts ( { results , id , items , action } ) { const extra = items . length - Object . keys ( results ) . length > 1 ? i18n . translate ( 'xpack.transform.models.transformService.allOtherRequestsCancelledDescription' , { defaultMessage : 'All other requests cancelled.' , } ) : '' ; const reason = i18n . translate ( 'xpack.transform.models.transformService.requestToActionTimedOutErrorMessage' , { defaultMessage : `Request to {action} '{id}' timed out. {extra}` , values : { id , action , extra , } , } ) ; const error = { reason , root_cause : [ { reason , } , ] , } ; const newResults = { } ; return items . reduce ( ( accumResults , currentVal ) => { if ( results [ currentVal . id ] === undefined ) { accumResults [ currentVal . id ] = { success : false , error , } ; } else { accumResults [ currentVal . id ] = results [ currentVal . id ] ; } return accumResults ; } , newResults ) ; } export function wrapError ( error ) { const boom = Boom . isBoom ( error ) ? error : Boom . boomify ( error , { statusCode : error . statusCode } ) ; return { body : boom , headers : boom . output . headers as { [ key ] : string ; } , statusCode : boom . output . statusCode , } ; } function extractCausedByChain ( causedBy = { } , accumulator = [ ] ) { const { reason , caused_by } = causedBy ; if ( reason ) { accumulator . push ( reason ) ; } if ( caused_by ) { return extractCausedByChain ( caused_by , accumulator ) ; } return accumulator ; } param err param statusCodeToMessageMap return export function wrapEsError ( err , statusCodeToMessageMap = { } ) { const { meta : { body , statusCode } , } = err ; const { error : { root_cause = [ ] , caused_by = { } , } = { } , } = body ; if ( ! statusCodeToMessageMap [ statusCode ] ) { const boomError = Boom . boomify ( err , { statusCode } ) ; const causedByChain = extractCausedByChain ( caused_by ) ; const defaultCause = root_cause . length ? extractCausedByChain ( root_cause [ 0 ] ) : undefined ; boomError . output . payload . cause = causedByChain . length ? causedByChain : defaultCause ; if ( root_cause ?. [ 0 ] ) { boomError . message = extractErrorMessage ( root_cause [ 0 ] ) ; } return boomError ; } const message = statusCodeToMessageMap [ statusCode ] ; return new Boom . Boom ( message , { statusCode } ) ; } interface EsError { type : string ; reason : string ; line ? : number ; col ? : number ; script ? : string ; } /**
 * Returns an error message based on the root cause
 */ function extractErrorMessage ( { type , reason , script , line , col } ) { let message = `[${ type }] ${ reason }` ; if ( line !== undefined && col !== undefined ) { message += `, with line=${ line } & col=${ col }` ; } if ( script !== undefined ) { message += ` '${ script }'` ; } return message ; }