Skip to content

Commit

Permalink
[ML] Catching unknown index pattern errors
Browse files Browse the repository at this point in the history
  • Loading branch information
jgowdyelastic committed Mar 31, 2020
1 parent 1696dd5 commit aa6bfbe
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Original file line number Diff line number Diff line change
@@ -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;
};
26 changes: 21 additions & 5 deletions x-pack/plugins/ml/public/application/routing/use_resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import { useEffect, useState } from 'react';
import { IUiSettingsClient } from 'kibana/public';
import { i18n } from '@kbn/i18n';
import {
getIndexPatternById,
getIndexPatternsContract,
Expand All @@ -14,13 +15,16 @@ 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,
savedSearchId: string | undefined,
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) => {
Expand All @@ -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.
Expand All @@ -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({});
}
})();
}, []);
Expand Down

0 comments on commit aa6bfbe

Please sign in to comment.