From 23b5355ebd3679e42878098ebabbcd9d19fbae73 Mon Sep 17 00:00:00 2001 From: Dima Arnautov Date: Wed, 18 Aug 2021 14:51:34 +0200 Subject: [PATCH] [ML] guards for uiSettings and fieldRegistry, add custom errors --- .../ml/server/shared_services/errors.ts | 16 +++++--- .../server/shared_services/shared_services.ts | 37 ++++++++++++++++--- 2 files changed, 41 insertions(+), 12 deletions(-) diff --git a/x-pack/plugins/ml/server/shared_services/errors.ts b/x-pack/plugins/ml/server/shared_services/errors.ts index 4b8e6625c5aef..eb9bbbb40fbd7 100644 --- a/x-pack/plugins/ml/server/shared_services/errors.ts +++ b/x-pack/plugins/ml/server/shared_services/errors.ts @@ -5,9 +5,13 @@ * 2.0. */ -export class MLClusterClientUninitialized extends Error { - constructor(message?: string) { - super(message); - Object.setPrototypeOf(this, new.target.prototype); - } -} +export const getCustomError = (className: string, errorMessage: string) => { + const C = class extends Error { + constructor(message?: string) { + super(message); + Object.setPrototypeOf(this, new.target.prototype); + } + }; + Object.defineProperty(C, 'name', { value: className }); + return new C(errorMessage); +}; diff --git a/x-pack/plugins/ml/server/shared_services/shared_services.ts b/x-pack/plugins/ml/server/shared_services/shared_services.ts index 4c1f7e491c7f7..a9f92514d27c1 100644 --- a/x-pack/plugins/ml/server/shared_services/shared_services.ts +++ b/x-pack/plugins/ml/server/shared_services/shared_services.ts @@ -28,7 +28,7 @@ import { } from './providers/anomaly_detectors'; import { ResolveMlCapabilities, MlCapabilitiesKey } from '../../common/types/capabilities'; import { hasMlCapabilitiesProvider, HasMlCapabilities } from '../lib/capabilities'; -import { MLClusterClientUninitialized } from './errors'; +import { getCustomError } from './errors'; import { MlClient, getMlClient } from '../lib/ml_client'; import { jobSavedObjectServiceFactory, JobSavedObjectService } from '../saved_objects'; import { @@ -191,14 +191,39 @@ function getRequestItemsProvider( isMlReady ); - const uiSettingsClient = getUiSettings()!.asScopedToClient(savedObjectsClient); - const getFieldsFormatRegistry = () => - getFieldsFormat()!.fieldFormatServiceFactory(uiSettingsClient); - if (clusterClient === null) { - throw new MLClusterClientUninitialized(`ML's cluster client has not been initialized`); + throw getCustomError( + 'MLClusterClientUninitialized', + `ML's cluster client has not been initialized` + ); } + const uiSettingsClient = getUiSettings()!.asScopedToClient(savedObjectsClient); + if (uiSettingsClient === null) { + throw getCustomError( + 'MLUISettingsClientUninitialized', + `ML's UI settings client has not been initialized` + ); + } + + const getFieldsFormatRegistry = async () => { + let fieldFormatRegistry; + try { + fieldFormatRegistry = await getFieldsFormat()!.fieldFormatServiceFactory(uiSettingsClient); + } catch (e) { + // throw an custom error during the fieldFormatRegistry check + } + + if (!fieldFormatRegistry) { + throw getCustomError( + 'MLFieldFormatRegistryUninitialized', + `ML's field format registry has not been initialized` + ); + } + + return fieldFormatRegistry; + }; + if (request instanceof KibanaRequest) { hasMlCapabilities = getHasMlCapabilities(request); scopedClient = clusterClient.asScoped(request);