-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
Copy pathformat_msg.ts
66 lines (60 loc) · 2.25 KB
/
format_msg.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
import _ from 'lodash';
import { i18n } from '@kbn/i18n';
import { formatESMsg } from './format_es_msg';
const has = _.has;
/**
* Formats the error message from an error object, extended elasticsearch
* object or simple string; prepends optional second parameter to the message
* @param {Error|String} err
* @param {String} source - Prefix for message indicating source (optional)
* @returns {string}
*/
export function formatMsg(err: Record<string, any> | string, source: string = '') {
let message = '';
if (source) {
message += source + ': ';
}
const esMsg = formatESMsg(err);
if (typeof err === 'string') {
message += err;
} else if (esMsg) {
message += esMsg;
} else if (err instanceof Error) {
message += formatMsg.describeError(err);
} else if (has(err, 'status') && has(err, 'data')) {
// is an Angular $http "error object"
if (err.status === -1) {
// status = -1 indicates that the request was failed to reach the server
message += i18n.translate('kibana_legacy.notify.toaster.unavailableServerErrorMessage', {
defaultMessage:
'An HTTP request has failed to connect. ' +
'Please check if the Kibana server is running and that your browser has a working connection, ' +
'or contact your system administrator.',
});
} else {
message += i18n.translate('kibana_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: Record<string, any>) {
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;
};