2020-10-13 15:17:40 +02:00
|
|
|
/*
|
|
|
|
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
2021-02-04 03:12:39 +01:00
|
|
|
* or more contributor license agreements. Licensed under the Elastic License
|
|
|
|
* 2.0; you may not use this file except in compliance with the Elastic License
|
|
|
|
* 2.0.
|
2020-10-13 15:17:40 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
import { i18n } from '@kbn/i18n';
|
|
|
|
|
|
|
|
import { ExpressionRenderError } from 'src/plugins/expressions/public';
|
|
|
|
|
|
|
|
interface ElasticsearchErrorClause {
|
|
|
|
type: string;
|
|
|
|
reason: string;
|
|
|
|
caused_by?: ElasticsearchErrorClause;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface RequestError extends Error {
|
|
|
|
body?: { attributes?: { error: ElasticsearchErrorClause } };
|
|
|
|
}
|
|
|
|
|
|
|
|
const isRequestError = (e: Error | RequestError): e is RequestError => {
|
|
|
|
if ('body' in e) {
|
|
|
|
return e.body?.attributes?.error?.caused_by !== undefined;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2021-03-17 14:26:43 +01:00
|
|
|
interface ESError extends Error {
|
|
|
|
attributes?: { caused_by?: ElasticsearchErrorClause };
|
|
|
|
}
|
|
|
|
|
|
|
|
const isEsError = (e: Error | ESError): e is ESError => {
|
|
|
|
if ('attributes' in e) {
|
|
|
|
return e.attributes?.caused_by?.caused_by !== undefined;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2020-10-13 15:17:40 +02:00
|
|
|
function getNestedErrorClause({
|
|
|
|
type,
|
|
|
|
reason,
|
|
|
|
caused_by: causedBy,
|
|
|
|
}: ElasticsearchErrorClause): { type: string; reason: string } {
|
|
|
|
if (causedBy) {
|
|
|
|
return getNestedErrorClause(causedBy);
|
|
|
|
}
|
|
|
|
return { type, reason };
|
|
|
|
}
|
|
|
|
|
2021-03-17 14:26:43 +01:00
|
|
|
function getErrorSource(e: Error | RequestError | ESError) {
|
|
|
|
if (isRequestError(e)) {
|
|
|
|
return e.body!.attributes!.error;
|
|
|
|
}
|
|
|
|
if (isEsError(e)) {
|
|
|
|
return e.attributes!.caused_by;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-13 15:17:40 +02:00
|
|
|
export function getOriginalRequestErrorMessage(error?: ExpressionRenderError | null) {
|
2021-03-17 14:26:43 +01:00
|
|
|
if (error && 'original' in error && error.original) {
|
|
|
|
const errorSource = getErrorSource(error.original);
|
|
|
|
if (errorSource == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const rootError = getNestedErrorClause(errorSource);
|
2020-10-13 15:17:40 +02:00
|
|
|
if (rootError.reason && rootError.type) {
|
|
|
|
return i18n.translate('xpack.lens.editorFrame.expressionFailureMessage', {
|
|
|
|
defaultMessage: 'Request error: {type}, {reason}',
|
|
|
|
values: {
|
|
|
|
reason: rootError.reason,
|
|
|
|
type: rootError.type,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-02-15 13:00:31 +01:00
|
|
|
|
|
|
|
export function getMissingVisualizationTypeError() {
|
|
|
|
return i18n.translate('xpack.lens.editorFrame.expressionMissingVisualizationType', {
|
|
|
|
defaultMessage: 'Visualization type not found.',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getMissingCurrentDatasource() {
|
|
|
|
return i18n.translate('xpack.lens.editorFrame.expressionMissingDatasource', {
|
|
|
|
defaultMessage: 'Could not find datasource for the visualization',
|
|
|
|
});
|
|
|
|
}
|
2021-03-18 14:29:17 +01:00
|
|
|
|
|
|
|
export function getMissingIndexPatterns(indexPatternIds: string[]) {
|
|
|
|
return i18n.translate('xpack.lens.editorFrame.expressionMissingIndexPattern', {
|
|
|
|
defaultMessage:
|
|
|
|
'Could not find the {count, plural, one {index pattern} other {index pattern}}: {ids}',
|
|
|
|
values: { count: indexPatternIds.length, ids: indexPatternIds.join(', ') },
|
|
|
|
});
|
|
|
|
}
|