import _ from 'lodash' ; import { i18n } from '@osd/i18n' ; import { formatOpenSearchMsg } from './format_opensearch_msg' ; const has = _ . has ; param { Error | String } err param { String } source returns { string } export function formatMsg ( err , source = '' ) { let message = '' ; if ( source ) { message += source + ': ' ; } const opensearchMsg = formatOpenSearchMsg ( err ) ; if ( typeof err === 'string' ) { message += err ; } else if ( opensearchMsg ) { message += opensearchMsg ; } else if ( err instanceof Error ) { message += formatMsg . describeError ( err ) ; } else if ( has ( err , 'status' ) && has ( err , 'data' ) ) { if ( err . status === - 1 ) { message += i18n . translate ( 'opensearch_dashboards_legacy.notify.toaster.unavailableServerErrorMessage' , { defaultMessage : 'An HTTP request has failed to connect. ' + 'Please check if the OpenSearch Dashboards server is running and that your browser has a working connection, ' + 'or contact your system administrator.' , } ) ; } else { message += i18n . translate ( 'opensearch_dashboards_legacy.notify.toaster.errorStatusMessage' , { defaultMessage : 'Error {errStatus} {errStatusText}: {errMessage}' , values : { errStatus : err . status , errStatusText : err . statusText , errMessage : err . data . message , } , } ) ; } } return message ; } formatMsg . describeError = function ( err ) { if ( ! err ) return undefined ; if ( err . shortMessage ) return err . shortMessage ; if ( err . body && err . body . message ) return err . body . message ; if ( err . message ) return err . message ; return '' + err ; } ;