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

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

* Cleanup i18n

* Update error messages
This commit is contained in:
Mike Côté 2021-04-23 07:05:51 -04:00 committed by GitHub
parent 1dd5e1f13d
commit 7598f57bcb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 24 additions and 43 deletions

View file

@ -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', () => {

View file

@ -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<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({

View file

@ -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);
@ -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);

View file

@ -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": "アラートフレームワークを使用できません",

View file

@ -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": "告警框架不可用",