Skip to content

Commit

Permalink
[ML] create classes for custom errors
Browse files Browse the repository at this point in the history
  • Loading branch information
darnautov committed Aug 19, 2021
1 parent 4601d31 commit f37aa84
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 19 deletions.
42 changes: 36 additions & 6 deletions x-pack/plugins/ml/server/shared_services/errors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
14 changes: 12 additions & 2 deletions x-pack/plugins/ml/server/shared_services/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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'
);
19 changes: 8 additions & 11 deletions x-pack/plugins/ml/server/shared_services/shared_services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 () => {
Expand All @@ -215,8 +213,7 @@ function getRequestItemsProvider(
}

if (!fieldFormatRegistry) {
throw getCustomError(
'MLFieldFormatRegistryUninitialized',
throw new MLFieldFormatRegistryUninitialized(
`ML's field format registry has not been initialized`
);
}
Expand Down

0 comments on commit f37aa84

Please sign in to comment.