Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ML] Catching unknown index pattern errors #61935

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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',
}),
});
}
Expand Down
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 occurred',
}),
});
window.location.href = '#/';
}
} catch (error) {
// quietly fail. Let the resolvers handle the redirection if any fail to resolve
} else {
setContext({});
}
})();
}, []);
Expand Down