Skip to content

Commit

Permalink
[Lens] Improves error messages when in Dashboard (#90668)
Browse files Browse the repository at this point in the history
Co-authored-by: Kibana Machine <[email protected]>
  • Loading branch information
dej611 and kibanamachine authored Feb 15, 2021
1 parent 0ee7be1 commit e9e7453
Show file tree
Hide file tree
Showing 24 changed files with 447 additions and 333 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -459,10 +459,10 @@ describe('Datatable Visualization', () => {
label: 'label',
});

const error = datatableVisualization.getErrorMessages(
{ layerId: 'a', columns: [{ columnId: 'b' }, { columnId: 'c' }] },
frame
);
const error = datatableVisualization.getErrorMessages({
layerId: 'a',
columns: [{ columnId: 'b' }, { columnId: 'c' }],
});

expect(error).toBeUndefined();
});
Expand All @@ -478,10 +478,10 @@ describe('Datatable Visualization', () => {
label: 'label',
});

const error = datatableVisualization.getErrorMessages(
{ layerId: 'a', columns: [{ columnId: 'b' }, { columnId: 'c' }] },
frame
);
const error = datatableVisualization.getErrorMessages({
layerId: 'a',
columns: [{ columnId: 'b' }, { columnId: 'c' }],
});

expect(error).toBeUndefined();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ export const datatableVisualization: Visualization<DatatableVisualizationState>
};
},

getErrorMessages(state, frame) {
getErrorMessages(state) {
return undefined;
},

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ import {
import { buildExpression } from './expression_helpers';
import { Document } from '../../persistence/saved_object_store';
import { VisualizeFieldContext } from '../../../../../../src/plugins/ui_actions/public';
import { getActiveDatasourceIdFromDoc } from './state_management';
import { ErrorMessage } from '../types';
import { getMissingCurrentDatasource, getMissingVisualizationTypeError } from '../error_helper';

export async function initializeDatasources(
datasourceMap: Record<string, Datasource>,
Expand Down Expand Up @@ -72,15 +75,20 @@ export async function persistedStateToExpression(
datasources: Record<string, Datasource>,
visualizations: Record<string, Visualization>,
doc: Document
): Promise<Ast | null> {
): Promise<{ ast: Ast | null; errors: ErrorMessage[] | undefined }> {
const {
state: { visualization: visualizationState, datasourceStates: persistedDatasourceStates },
visualizationType,
references,
title,
description,
} = doc;
if (!visualizationType) return null;
if (!visualizationType) {
return {
ast: null,
errors: [{ shortMessage: '', longMessage: getMissingVisualizationTypeError() }],
};
}
const visualization = visualizations[visualizationType!];
const datasourceStates = await initializeDatasources(
datasources,
Expand All @@ -97,29 +105,42 @@ export async function persistedStateToExpression(

const datasourceLayers = createDatasourceLayers(datasources, datasourceStates);

return buildExpression({
title,
description,
const datasourceId = getActiveDatasourceIdFromDoc(doc);
if (datasourceId == null) {
return {
ast: null,
errors: [{ shortMessage: '', longMessage: getMissingCurrentDatasource() }],
};
}
const validationResult = validateDatasourceAndVisualization(
datasources[datasourceId],
datasourceStates[datasourceId].state,
visualization,
visualizationState,
datasourceMap: datasources,
datasourceStates,
datasourceLayers,
});
{ datasourceLayers }
);

return {
ast: buildExpression({
title,
description,
visualization,
visualizationState,
datasourceMap: datasources,
datasourceStates,
datasourceLayers,
}),
errors: validationResult,
};
}

export const validateDatasourceAndVisualization = (
currentDataSource: Datasource | null,
currentDatasourceState: unknown | null,
currentVisualization: Visualization | null,
currentVisualizationState: unknown | undefined,
frameAPI: FramePublicAPI
):
| Array<{
shortMessage: string;
longMessage: string;
}>
| undefined => {
frameAPI: Pick<FramePublicAPI, 'datasourceLayers'>
): ErrorMessage[] | undefined => {
const layersGroups = currentVisualizationState
? currentVisualization
?.getLayerIds(currentVisualizationState)
Expand All @@ -141,7 +162,7 @@ export const validateDatasourceAndVisualization = (
: undefined;

const visualizationValidationErrors = currentVisualizationState
? currentVisualization?.getErrorMessages(currentVisualizationState, frameAPI)
? currentVisualization?.getErrorMessages(currentVisualizationState)
: undefined;

if (datasourceValidationErrors?.length || visualizationValidationErrors?.length) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,7 @@ export const WorkspacePanel = React.memo(function WorkspacePanel({
datasourceLayers: framePublicAPI.datasourceLayers,
});
} catch (e) {
const buildMessages = activeVisualization?.getErrorMessages(
visualizationState,
framePublicAPI
);
const buildMessages = activeVisualization?.getErrorMessages(visualizationState);
const defaultMessage = {
shortMessage: i18n.translate('xpack.lens.editorFrame.buildExpressionError', {
defaultMessage: 'An unexpected error occurred while preparing the chart',
Expand Down
Loading

0 comments on commit e9e7453

Please sign in to comment.