Skip to content

Commit

Permalink
[ML] guards for uiSettings and fieldRegistry, add custom errors
Browse files Browse the repository at this point in the history
  • Loading branch information
darnautov committed Aug 18, 2021
1 parent 32cc1cf commit 23b5355
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 12 deletions.
16 changes: 10 additions & 6 deletions x-pack/plugins/ml/server/shared_services/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
37 changes: 31 additions & 6 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,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 {
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 23b5355

Please sign in to comment.