From aa6bfbe4f8c8f7785444eb3981a13551c0e774c7 Mon Sep 17 00:00:00 2001 From: James Gowdy Date: Tue, 31 Mar 2020 09:34:41 +0100 Subject: [PATCH 1/3] [ML] Catching unknown index pattern errors --- .../application/contexts/kibana/index.ts | 1 + .../kibana/use_notifications_context.ts | 11 ++++++++ .../application/routing/use_resolver.ts | 26 +++++++++++++++---- 3 files changed, 33 insertions(+), 5 deletions(-) create mode 100644 x-pack/plugins/ml/public/application/contexts/kibana/use_notifications_context.ts diff --git a/x-pack/plugins/ml/public/application/contexts/kibana/index.ts b/x-pack/plugins/ml/public/application/contexts/kibana/index.ts index 2add1b6ea161c..0f071a42a5688 100644 --- a/x-pack/plugins/ml/public/application/contexts/kibana/index.ts +++ b/x-pack/plugins/ml/public/application/contexts/kibana/index.ts @@ -7,3 +7,4 @@ export { useMlKibana, StartServices, MlKibanaReactContextValue } from './kibana_context'; export { useUiSettings } from './use_ui_settings_context'; export { useTimefilter } from './use_timefilter'; +export { useNotifications } from './use_notifications_context'; diff --git a/x-pack/plugins/ml/public/application/contexts/kibana/use_notifications_context.ts b/x-pack/plugins/ml/public/application/contexts/kibana/use_notifications_context.ts new file mode 100644 index 0000000000000..a03d9da49f365 --- /dev/null +++ b/x-pack/plugins/ml/public/application/contexts/kibana/use_notifications_context.ts @@ -0,0 +1,11 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import { useMlKibana } from './kibana_context'; + +export const useNotifications = () => { + return useMlKibana().services.notifications; +}; diff --git a/x-pack/plugins/ml/public/application/routing/use_resolver.ts b/x-pack/plugins/ml/public/application/routing/use_resolver.ts index 6df7eee3d64a6..3e3b8c97f970e 100644 --- a/x-pack/plugins/ml/public/application/routing/use_resolver.ts +++ b/x-pack/plugins/ml/public/application/routing/use_resolver.ts @@ -6,6 +6,7 @@ import { useEffect, useState } from 'react'; import { IUiSettingsClient } from 'kibana/public'; +import { i18n } from '@kbn/i18n'; import { getIndexPatternById, getIndexPatternsContract, @@ -14,6 +15,7 @@ import { import { createSearchItems } from '../jobs/new_job/utils/new_job_utils'; import { ResolverResults, Resolvers } from './resolvers'; import { MlContextValue } from '../contexts/ml'; +import { useNotifications } from '../contexts/kibana'; export const useResolver = ( indexPatternId: string | undefined, @@ -21,6 +23,8 @@ export const useResolver = ( config: IUiSettingsClient, resolvers: Resolvers ): { context: MlContextValue; results: ResolverResults } => { + const notifications = useNotifications(); + const funcNames = Object.keys(resolvers); // Object.entries gets this wrong?! const funcs = Object.values(resolvers); // Object.entries gets this wrong?! const tempResults = funcNames.reduce((p, c) => { @@ -37,8 +41,14 @@ export const useResolver = ( const res = await Promise.all(funcs.map(r => r())); res.forEach((r, i) => (tempResults[funcNames[i]] = r)); setResults(tempResults); + } catch (error) { + // quietly fail. Errors being thrown here are expected as a way to handle privilege or license check failures. + // The user will be redirected by the failed resolver. + return; + } - if (indexPatternId !== undefined || savedSearchId !== undefined) { + if (indexPatternId !== undefined || savedSearchId !== undefined) { + try { // note, currently we're using our own kibana context that requires a current index pattern to be set // this means, if the page uses this context, useResolver must be passed a string for the index pattern id // and loadIndexPatterns must be part of the resolvers. @@ -56,11 +66,17 @@ export const useResolver = ( indexPatterns: getIndexPatternsContract()!, kibanaConfig: config, }); - } else { - setContext({}); + } catch (error) { + // an unexpected error has occurred. This could be caused by an incorrect index pattern or saved search ID + notifications.toasts.addError(new Error(error), { + title: i18n.translate('xpack.ml.useResolver.errorTitle', { + defaultMessage: 'An error has ocurred', + }), + }); + window.location.href = '#/'; } - } catch (error) { - // quietly fail. Let the resolvers handle the redirection if any fail to resolve + } else { + setContext({}); } })(); }, []); From 89897e05d6ca46c4873e01cdc33cd7187efeb044 Mon Sep 17 00:00:00 2001 From: James Gowdy Date: Tue, 31 Mar 2020 11:52:29 +0100 Subject: [PATCH 2/3] fixing typo --- .../application/components/messagebar/messagebar_service.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x-pack/plugins/ml/public/application/components/messagebar/messagebar_service.js b/x-pack/plugins/ml/public/application/components/messagebar/messagebar_service.js index d79fe14cbac4e..1dc7140bd2687 100644 --- a/x-pack/plugins/ml/public/application/components/messagebar/messagebar_service.js +++ b/x-pack/plugins/ml/public/application/components/messagebar/messagebar_service.js @@ -21,7 +21,7 @@ function errorNotify(text, resp) { const toastNotifications = getToastNotifications(); toastNotifications.addError(new MLRequestFailure(err, resp), { title: i18n.translate('xpack.ml.messagebarService.errorTitle', { - defaultMessage: 'An error has ocurred', + defaultMessage: 'An error has occurred', }), }); } From 9719370816dc3c3d8205690d1e394f4a5d4989cb Mon Sep 17 00:00:00 2001 From: James Gowdy Date: Tue, 31 Mar 2020 12:34:18 +0100 Subject: [PATCH 3/3] fixing typo --- x-pack/plugins/ml/public/application/routing/use_resolver.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x-pack/plugins/ml/public/application/routing/use_resolver.ts b/x-pack/plugins/ml/public/application/routing/use_resolver.ts index 3e3b8c97f970e..8e94f8d77fbb2 100644 --- a/x-pack/plugins/ml/public/application/routing/use_resolver.ts +++ b/x-pack/plugins/ml/public/application/routing/use_resolver.ts @@ -70,7 +70,7 @@ export const useResolver = ( // an unexpected error has occurred. This could be caused by an incorrect index pattern or saved search ID notifications.toasts.addError(new Error(error), { title: i18n.translate('xpack.ml.useResolver.errorTitle', { - defaultMessage: 'An error has ocurred', + defaultMessage: 'An error has occurred', }), }); window.location.href = '#/';