Skip to content

Commit

Permalink
Log instead of throwing error when alert type doesn't exist (elastic#…
Browse files Browse the repository at this point in the history
…98005)

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

* Cleanup i18n

* Update error messages
# Conflicts:
#	x-pack/plugins/translations/translations/ja-JP.json
#	x-pack/plugins/translations/translations/zh-CN.json
  • Loading branch information
mikecote committed Apr 23, 2021
1 parent ac14672 commit 5f7e65e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 41 deletions.
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

0 comments on commit 5f7e65e

Please sign in to comment.