[ML] Fix error handling for missing data view in data frame analytics wizard (#119455) (#119502)

* [ML] Fix error handling for missing data view in data frame analytics wizard

* [ML] Fix jest tests

Co-authored-by: Pete Harverson <pete@elastic.co>
This commit is contained in:
Kibana Machine 2021-11-23 16:22:37 -05:00 committed by GitHub
parent 3d083b6349
commit f61fdc82ce
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 2 deletions

View file

@ -76,6 +76,11 @@ jest.mock('../../../../../contexts/kibana', () => ({
},
}),
useNavigateToPath: () => mockNavigateToPath,
useNotifications: () => {
return {
toasts: { addSuccess: jest.fn(), addDanger: jest.fn(), addError: jest.fn() },
};
},
}));
jest.mock('../../../../../util/index_utils', () => {

View file

@ -23,6 +23,8 @@ import type { SimpleSavedObject } from 'src/core/public';
import { SavedObjectFinderUi } from '../../../../../../../../../../src/plugins/saved_objects/public';
import { useMlKibana, useNavigateToPath } from '../../../../../contexts/kibana';
import { useToastNotificationService } from '../../../../../services/toast_notification_service';
import { getNestedProperty } from '../../../../../util/object_utils';
import { getDataViewAndSavedSearch, isCcsIndexPattern } from '../../../../../util/index_utils';
@ -41,6 +43,7 @@ export const SourceSelection: FC<Props> = ({ onClose }) => {
const [isCcsCallOut, setIsCcsCallOut] = useState(false);
const [ccsCallOutBodyText, setCcsCallOutBodyText] = useState<string>();
const toastNotificationService = useToastNotificationService();
const onSearchSelected = async (
id: string,
@ -57,8 +60,22 @@ export const SourceSelection: FC<Props> = ({ onClose }) => {
if (type === 'index-pattern') {
dataViewName = getNestedProperty(savedObject, 'attributes.title');
} else if (type === 'search') {
const dataViewAndSavedSearch = await getDataViewAndSavedSearch(id);
dataViewName = dataViewAndSavedSearch.dataView?.title ?? '';
try {
const dataViewAndSavedSearch = await getDataViewAndSavedSearch(id);
dataViewName = dataViewAndSavedSearch.dataView?.title ?? '';
} catch (error) {
// an unexpected error has occurred. This could be caused by a saved search for which the data view no longer exists.
toastNotificationService.displayErrorToast(
error,
i18n.translate(
'xpack.ml.dataFrame.analytics.create.searchSelection.errorGettingDataViewTitle',
{
defaultMessage: 'Error loading data view used by the saved search',
}
)
);
return;
}
}
if (isCcsIndexPattern(dataViewName)) {