Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log instead of throwing error when alert type doesn't exist #98005

Merged
merged 3 commits into from
Apr 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 0 additions & 19 deletions x-pack/plugins/alerting/public/alert_api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,25 +78,6 @@ describe('loadAlertType', () => {

expect(await loadAlertType({ http, id: 'test-another' })).toEqual(alertType);
});

test('should throw if required alertType is missing', async () => {
http.get.mockResolvedValueOnce([
{
id: 'test-another',
name: 'Test Another',
actionVariables: [],
actionGroups: [{ id: 'default', name: 'Default' }],
defaultActionGroupId: 'default',
minimumLicenseRequired: 'basic',
recoveryActionGroup: RecoveredActionGroup,
producer: 'alerts',
},
]);

expect(loadAlertType({ http, id: 'test' })).rejects.toMatchInlineSnapshot(
`[Error: Alert type "test" is not registered.]`
);
});
});

describe('loadAlert', () => {
Expand Down
19 changes: 4 additions & 15 deletions x-pack/plugins/alerting/public/alert_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
*/

import { HttpSetup } from 'kibana/public';
import { i18n } from '@kbn/i18n';
import { LEGACY_BASE_ALERT_API_PATH } from '../common';
import type { Alert, AlertType } from '../common';

Expand All @@ -20,21 +19,11 @@ export async function loadAlertType({
}: {
http: HttpSetup;
id: AlertType['id'];
}): Promise<AlertType> {
const maybeAlertType = ((await http.get(
}): Promise<AlertType | undefined> {
const alertTypes = (await http.get(
`${LEGACY_BASE_ALERT_API_PATH}/list_alert_types`
)) as AlertType[]).find((type) => type.id === id);
if (!maybeAlertType) {
throw new Error(
i18n.translate('xpack.alerting.loadAlertType.missingAlertTypeError', {
defaultMessage: 'Alert type "{id}" is not registered.',
values: {
id,
},
})
);
}
return maybeAlertType;
)) as AlertType[];
return alertTypes.find((type) => type.id === id);
}

export async function loadAlert({
Expand Down
27 changes: 20 additions & 7 deletions x-pack/plugins/alerting/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,19 @@ export class AlertingPublicPlugin implements Plugin<PluginSetupContract, PluginS

const registerNavigation = async (
consumer: string,
alertType: string,
alertTypeId: string,
handler: AlertNavigationHandler
) =>
this.alertNavigationRegistry!.register(
consumer,
await loadAlertType({ http: core.http, id: alertType }),
handler
);
) => {
const alertType = await loadAlertType({ http: core.http, id: alertTypeId });
if (!alertType) {
// eslint-disable-next-line no-console
console.log(
`Unable to register navigation for alert type "${alertTypeId}" because it is not registered on the server side.`
);
return;
}
this.alertNavigationRegistry!.register(consumer, alertType, handler);
};

const registerDefaultNavigation = async (consumer: string, handler: AlertNavigationHandler) =>
this.alertNavigationRegistry!.registerDefault(consumer, handler);
Expand All @@ -54,6 +59,14 @@ export class AlertingPublicPlugin implements Plugin<PluginSetupContract, PluginS
const alert = await loadAlert({ http: core.http, alertId });
const alertType = await loadAlertType({ http: core.http, id: alert.alertTypeId });

if (!alertType) {
// eslint-disable-next-line no-console
console.log(
`Unable to get navigation for alert type "${alert.alertTypeId}" because it is not registered on the server side.`
);
return;
}

if (this.alertNavigationRegistry!.has(alert.consumer, alertType)) {
const navigationHandler = this.alertNavigationRegistry!.get(alert.consumer, alertType);
const state = navigationHandler(alert, alertType);
Expand Down
1 change: 0 additions & 1 deletion x-pack/plugins/translations/translations/ja-JP.json
Original file line number Diff line number Diff line change
Expand Up @@ -4948,7 +4948,6 @@
"xpack.alerting.appName": "アラート",
"xpack.alerting.builtinActionGroups.recovered": "回復済み",
"xpack.alerting.injectActionParams.email.kibanaFooterLinkText": "Kibana でアラートを表示",
"xpack.alerting.loadAlertType.missingAlertTypeError": "アラートタイプ「{id}」は登録されていません。",
"xpack.alerting.server.healthStatus.available": "アラートフレームワークを使用できます",
"xpack.alerting.server.healthStatus.degraded": "アラートフレームワークは劣化しました",
"xpack.alerting.server.healthStatus.unavailable": "アラートフレームワークを使用できません",
Expand Down
1 change: 0 additions & 1 deletion x-pack/plugins/translations/translations/zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -4979,7 +4979,6 @@
"xpack.alerting.appName": "告警",
"xpack.alerting.builtinActionGroups.recovered": "已恢复",
"xpack.alerting.injectActionParams.email.kibanaFooterLinkText": "在 Kibana 中查看告警",
"xpack.alerting.loadAlertType.missingAlertTypeError": "未注册告警类型“{id}”。",
"xpack.alerting.server.healthStatus.available": "告警框架可用",
"xpack.alerting.server.healthStatus.degraded": "告警框架已降级",
"xpack.alerting.server.healthStatus.unavailable": "告警框架不可用",
Expand Down