import { i18n } from '@kbn/i18n' ; import type { Panel } from '../../../common/types' ; type ErrorType = { reason : string ; caused_by ? : ErrorType ; } | string ; export type ErrorResponse = Error & Partial < { errBody : { error : ErrorType ; } | string ; } > ; const getErrorMessage = ( errBody , defaultMessage ? ) : string | undefined => { if ( typeof errBody === 'string' ) { return errBody ; } else { if ( errBody . caused_by ) { return getErrorMessage ( errBody . caused_by , errBody . reason ) ; } return errBody . reason || defaultMessage ; } } ; export const handleErrorResponse = ( panel ) => ( error ) => { const result = { } ; if ( error . errBody ) { const errorResponse = typeof error . errBody === 'string' ? error . errBody : getErrorMessage ( error . errBody . error ) ; result [ panel . id ] = { id : panel . id , error : errorResponse ?? i18n . translate ( 'visTypeTimeseries.handleErrorResponse.unexpectedError' , { defaultMessage : 'Unexpected error' , } ) , series : [ ] , } ; } return result ; } ;