Skip to content

Commit 4e95736

Browse files
nreesekibanamachineelasticmachine
authored
[dashboard] fix add panel fails when custom panel placement callback throws (#222446)
While working on #221314, I noticed that "Add panel" will silently fail if `getDashboardPanelPlacementSetting` throws. Dashboard should be more resilient since it does not control what may be provided from a registry. PR just adds a try/catch around the call to avoid breaking "Add panel". --------- Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com> Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
1 parent 3d1c131 commit 4e95736

File tree

3 files changed

+35
-6
lines changed

3 files changed

+35
-6
lines changed

src/platform/plugins/shared/dashboard/public/dashboard_api/layout_manager.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ import { DEFAULT_PANEL_HEIGHT, DEFAULT_PANEL_WIDTH } from '../../common/content_
3939
import { prefixReferencesFromPanel } from '../../common/dashboard_container/persistable_state/dashboard_container_references';
4040
import { dashboardClonePanelActionStrings } from '../dashboard_actions/_dashboard_actions_strings';
4141
import { getPanelAddedSuccessString } from '../dashboard_app/_dashboard_app_strings';
42-
import { getDashboardPanelPlacementSetting } from '../panel_placement/panel_placement_registry';
42+
import { getPanelPlacementSetting } from '../panel_placement/get_panel_placement_settings';
4343
import { placeClonePanel } from '../panel_placement/place_clone_panel_strategy';
4444
import { runPanelPlacementStrategy } from '../panel_placement/place_new_panel_strategies';
4545
import { PanelPlacementStrategy } from '../plugin_constants';
@@ -165,10 +165,7 @@ export function initializeLayoutManager(
165165
},
166166
};
167167
}
168-
const getCustomPlacementSettingFunc = getDashboardPanelPlacementSetting(type);
169-
const customPlacementSettings = getCustomPlacementSettingFunc
170-
? await getCustomPlacementSettingFunc(serializedState)
171-
: undefined;
168+
const customPlacementSettings = await getPanelPlacementSetting(type, serializedState);
172169
const { newPanelPlacement, otherPanels } = runPanelPlacementStrategy(
173170
customPlacementSettings?.strategy ?? PanelPlacementStrategy.findTopLeftMostOpenSpace,
174171
{
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the "Elastic License
4+
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
5+
* Public License v 1"; you may not use this file except in compliance with, at
6+
* your election, the "Elastic License 2.0", the "GNU Affero General Public
7+
* License v3.0 only", or the "Server Side Public License, v 1".
8+
*/
9+
10+
import { SerializedPanelState } from '@kbn/presentation-publishing';
11+
import { getRegistryItem } from './panel_placement_registry';
12+
import { PanelPlacementSettings } from './types';
13+
14+
export async function getPanelPlacementSetting(
15+
embeddableType: string,
16+
serializedState?: SerializedPanelState<object>
17+
): Promise<undefined | PanelPlacementSettings> {
18+
const registryItem = getRegistryItem(embeddableType);
19+
if (!registryItem) return;
20+
21+
try {
22+
return await registryItem(serializedState);
23+
} catch (e) {
24+
// eslint-disable-next-line no-console
25+
console.warn(
26+
`Unable to get panel placement settings; embeddableType: ${embeddableType}, serializedState: ${serializedState}, error: ${e}`
27+
);
28+
}
29+
}

src/platform/plugins/shared/dashboard/public/panel_placement/panel_placement_registry.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ export const registerDashboardPanelPlacementSetting = <SerializedState extends o
2727
registry.set(embeddableType, getPanelPlacementSettings as GetPanelPlacementSettings<object>);
2828
};
2929

30-
export const getDashboardPanelPlacementSetting = (embeddableType: string) => {
30+
/**
31+
* Use getPanelPlacementSetting to access registry
32+
*/
33+
export const getRegistryItem = (embeddableType: string) => {
3134
return registry.get(embeddableType);
3235
};

0 commit comments

Comments
 (0)