kibana/x-pack/plugins/lens/public/editor_frame_service/error_helper.ts
Marco Liberati e9e7453e1d
[Lens] Improves error messages when in Dashboard (#90668)
Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
2021-02-15 13:00:31 +01:00

66 lines
1.9 KiB
TypeScript

/*
* 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; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
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,
},
});
}
}
}
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',
});
}