From aa6bfbe4f8c8f7785444eb3981a13551c0e774c7 Mon Sep 17 00:00:00 2001 From: James Gowdy Date: Tue, 31 Mar 2020 09:34:41 +0100 Subject: [PATCH] [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 2add1b6ea161c4..0f071a42a56889 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 00000000000000..a03d9da49f365c --- /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 6df7eee3d64a61..3e3b8c97f970e4 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({}); } })(); }, []);