From 94ea8b57a50a6056e06060f86c8a044218bae6d4 Mon Sep 17 00:00:00 2001 From: Mike Cote Date: Thu, 22 Apr 2021 09:17:21 -0400 Subject: [PATCH 1/3] Log instead of throwing error when alert type doesn't exist --- .../plugins/alerting/public/alert_api.test.ts | 19 --------------- x-pack/plugins/alerting/public/alert_api.ts | 19 ++++----------- x-pack/plugins/alerting/public/plugin.ts | 23 +++++++++++++------ 3 files changed, 20 insertions(+), 41 deletions(-) diff --git a/x-pack/plugins/alerting/public/alert_api.test.ts b/x-pack/plugins/alerting/public/alert_api.test.ts index 152097d084eb8..023ea255e1c42 100644 --- a/x-pack/plugins/alerting/public/alert_api.test.ts +++ b/x-pack/plugins/alerting/public/alert_api.test.ts @@ -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', () => { diff --git a/x-pack/plugins/alerting/public/alert_api.ts b/x-pack/plugins/alerting/public/alert_api.ts index d1213c80b95be..f3faa65a4b384 100644 --- a/x-pack/plugins/alerting/public/alert_api.ts +++ b/x-pack/plugins/alerting/public/alert_api.ts @@ -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'; @@ -20,21 +19,11 @@ export async function loadAlertType({ }: { http: HttpSetup; id: AlertType['id']; -}): Promise { - const maybeAlertType = ((await http.get( +}): Promise { + 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({ diff --git a/x-pack/plugins/alerting/public/plugin.ts b/x-pack/plugins/alerting/public/plugin.ts index 5ca3c0af26801..5cf2bca63ad1f 100644 --- a/x-pack/plugins/alerting/public/plugin.ts +++ b/x-pack/plugins/alerting/public/plugin.ts @@ -30,14 +30,17 @@ export class AlertingPublicPlugin implements Plugin - 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(`Alert type "${alertTypeId}" is not registered.`); + return; + } + this.alertNavigationRegistry!.register(consumer, alertType, handler); + }; const registerDefaultNavigation = async (consumer: string, handler: AlertNavigationHandler) => this.alertNavigationRegistry!.registerDefault(consumer, handler); @@ -54,6 +57,12 @@ export class AlertingPublicPlugin implements Plugin Date: Thu, 22 Apr 2021 09:32:26 -0400 Subject: [PATCH 2/3] Cleanup i18n --- x-pack/plugins/translations/translations/ja-JP.json | 1 - x-pack/plugins/translations/translations/zh-CN.json | 1 - 2 files changed, 2 deletions(-) diff --git a/x-pack/plugins/translations/translations/ja-JP.json b/x-pack/plugins/translations/translations/ja-JP.json index a4fb733d20c62..87dd279ce19ff 100644 --- a/x-pack/plugins/translations/translations/ja-JP.json +++ b/x-pack/plugins/translations/translations/ja-JP.json @@ -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": "アラートフレームワークを使用できません", diff --git a/x-pack/plugins/translations/translations/zh-CN.json b/x-pack/plugins/translations/translations/zh-CN.json index c8f0f91108440..9acb7dbc35d52 100644 --- a/x-pack/plugins/translations/translations/zh-CN.json +++ b/x-pack/plugins/translations/translations/zh-CN.json @@ -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": "告警框架不可用", From 084e1382cec6aa834f30fc93cd67ad4b7256c726 Mon Sep 17 00:00:00 2001 From: Mike Cote Date: Thu, 22 Apr 2021 09:34:36 -0400 Subject: [PATCH 3/3] Update error messages --- x-pack/plugins/alerting/public/plugin.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/x-pack/plugins/alerting/public/plugin.ts b/x-pack/plugins/alerting/public/plugin.ts index 5cf2bca63ad1f..025467d92a6ac 100644 --- a/x-pack/plugins/alerting/public/plugin.ts +++ b/x-pack/plugins/alerting/public/plugin.ts @@ -36,7 +36,9 @@ export class AlertingPublicPlugin implements Plugin