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;
|
|
|
|
};
|
|
|
|
|
|
|
|
function getNestedErrorClause({
|
|
|
|
type,
|
|
|
|
reason,
|
|
|
|
caused_by: causedBy,
|
|
|
|
}: ElasticsearchErrorClause): { type: string; reason: string } {
|
|
|
|
if (causedBy) {
|
|
|
|
return getNestedErrorClause(causedBy);
|
|
|
|
}
|
|
|
|
return { type, reason };
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getOriginalRequestErrorMessage(error?: ExpressionRenderError | null) {
|
|
|
|
if (error && 'original' in error && error.original && isRequestError(error.original)) {
|
|
|
|
const rootError = getNestedErrorClause(error.original.body!.attributes!.error);
|
|
|
|
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',
|
|
|
|
});
|
|
|
|
}
|