From f37aa84e63345660e31f601c81a3035438cc5733 Mon Sep 17 00:00:00 2001 From: Dima Arnautov Date: Thu, 19 Aug 2021 11:22:14 +0200 Subject: [PATCH] [ML] create classes for custom errors --- .../ml/server/shared_services/errors.test.ts | 42 ++++++++++++++++--- .../ml/server/shared_services/errors.ts | 14 ++++++- .../server/shared_services/shared_services.ts | 19 ++++----- 3 files changed, 56 insertions(+), 19 deletions(-) diff --git a/x-pack/plugins/ml/server/shared_services/errors.test.ts b/x-pack/plugins/ml/server/shared_services/errors.test.ts index 9b5492bd7c6b6..727012595dff3 100644 --- a/x-pack/plugins/ml/server/shared_services/errors.test.ts +++ b/x-pack/plugins/ml/server/shared_services/errors.test.ts @@ -5,14 +5,44 @@ * 2.0. */ -import { getCustomError } from './errors'; +import { + getCustomErrorClass, + MLClusterClientUninitialized, + MLUISettingsClientUninitialized, + MLFieldFormatRegistryUninitialized, +} from './errors'; -describe('getCustomError', () => { +describe('Custom errors', () => { test('creates a custom error instance', () => { - const error = getCustomError('MLCustomError', 'farequote is not defined'); - expect(error.message).toBe('farequote is not defined'); - expect(error.name).toBe('MLCustomError'); + const MLCustomError = getCustomErrorClass('MLCustomError'); + const errorInstance = new MLCustomError('farequote is not defined'); + expect(errorInstance.message).toBe('farequote is not defined'); + expect(errorInstance.name).toBe('MLCustomError'); + expect(errorInstance).toBeInstanceOf(MLCustomError); // make sure that custom class extends Error - expect(error).toBeInstanceOf(Error); + expect(errorInstance).toBeInstanceOf(Error); + }); + + test('MLClusterClientUninitialized', () => { + const errorInstance = new MLClusterClientUninitialized('cluster client is not initialized'); + expect(errorInstance.message).toBe('cluster client is not initialized'); + expect(errorInstance.name).toBe('MLClusterClientUninitialized'); + expect(errorInstance).toBeInstanceOf(MLClusterClientUninitialized); + }); + + test('MLUISettingsClientUninitialized', () => { + const errorInstance = new MLUISettingsClientUninitialized('cluster client is not initialized'); + expect(errorInstance.message).toBe('cluster client is not initialized'); + expect(errorInstance.name).toBe('MLUISettingsClientUninitialized'); + expect(errorInstance).toBeInstanceOf(MLUISettingsClientUninitialized); + }); + + test('MLFieldFormatRegistryUninitialized', () => { + const errorInstance = new MLFieldFormatRegistryUninitialized( + 'cluster client is not initialized' + ); + expect(errorInstance.message).toBe('cluster client is not initialized'); + expect(errorInstance.name).toBe('MLFieldFormatRegistryUninitialized'); + expect(errorInstance).toBeInstanceOf(MLFieldFormatRegistryUninitialized); }); }); diff --git a/x-pack/plugins/ml/server/shared_services/errors.ts b/x-pack/plugins/ml/server/shared_services/errors.ts index 7d74c7090674d..39c629ad50f5f 100644 --- a/x-pack/plugins/ml/server/shared_services/errors.ts +++ b/x-pack/plugins/ml/server/shared_services/errors.ts @@ -5,7 +5,7 @@ * 2.0. */ -export const getCustomError = (className: string, errorMessage: string) => { +export const getCustomErrorClass = (className: string) => { const CustomError = class extends Error { constructor(message?: string) { super(message); @@ -16,5 +16,15 @@ export const getCustomError = (className: string, errorMessage: string) => { }; // set class name dynamically Object.defineProperty(CustomError, 'name', { value: className }); - return new CustomError(errorMessage); + return CustomError; }; + +export const MLClusterClientUninitialized = getCustomErrorClass('MLClusterClientUninitialized'); + +export const MLUISettingsClientUninitialized = getCustomErrorClass( + 'MLUISettingsClientUninitialized' +); + +export const MLFieldFormatRegistryUninitialized = getCustomErrorClass( + 'MLFieldFormatRegistryUninitialized' +); 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 dd7ff9271e683..5c8bbffe10aed 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,11 @@ import { } from './providers/anomaly_detectors'; import { ResolveMlCapabilities, MlCapabilitiesKey } from '../../common/types/capabilities'; import { hasMlCapabilitiesProvider, HasMlCapabilities } from '../lib/capabilities'; -import { getCustomError } from './errors'; +import { + MLClusterClientUninitialized, + MLFieldFormatRegistryUninitialized, + MLUISettingsClientUninitialized, +} from './errors'; import { MlClient, getMlClient } from '../lib/ml_client'; import { jobSavedObjectServiceFactory, JobSavedObjectService } from '../saved_objects'; import { @@ -192,18 +196,12 @@ function getRequestItemsProvider( ); if (clusterClient === null) { - throw getCustomError( - 'MLClusterClientUninitialized', - `ML's cluster client has not been initialized` - ); + throw new MLClusterClientUninitialized(`ML's cluster client has not been initialized`); } const uiSettingsClient = getUiSettings()?.asScopedToClient(savedObjectsClient); if (!uiSettingsClient) { - throw getCustomError( - 'MLUISettingsClientUninitialized', - `ML's UI settings client has not been initialized` - ); + throw new MLUISettingsClientUninitialized(`ML's UI settings client has not been initialized`); } const getFieldsFormatRegistry = async () => { @@ -215,8 +213,7 @@ function getRequestItemsProvider( } if (!fieldFormatRegistry) { - throw getCustomError( - 'MLFieldFormatRegistryUninitialized', + throw new MLFieldFormatRegistryUninitialized( `ML's field format registry has not been initialized` ); }