From 077ca249b8864c65aa056f79e4e163fa5fc62a9f Mon Sep 17 00:00:00 2001 From: Marshall Main Date: Thu, 12 Aug 2021 22:18:37 -0700 Subject: [PATCH] Swap right and left, wrap initializeService result --- .../rule_data_client/rule_data_client.ts | 10 ++++---- .../rule_data_plugin_service.ts | 24 +++++++++---------- 2 files changed, 16 insertions(+), 18 deletions(-) diff --git a/x-pack/plugins/rule_registry/server/rule_data_client/rule_data_client.ts b/x-pack/plugins/rule_registry/server/rule_data_client/rule_data_client.ts index 0d033448ed842..bbfa6abdd1a71 100644 --- a/x-pack/plugins/rule_registry/server/rule_data_client/rule_data_client.ts +++ b/x-pack/plugins/rule_registry/server/rule_data_client/rule_data_client.ts @@ -24,7 +24,7 @@ interface ConstructorOptions { waitUntilReadyForWriting: Promise; } -export type WaitResult = Either; +export type WaitResult = Either; export class RuleDataClient implements IRuleDataClient { constructor(private readonly options: ConstructorOptions) {} @@ -44,9 +44,9 @@ export class RuleDataClient implements IRuleDataClient { const waitUntilReady = async () => { const result = await this.options.waitUntilReadyForReading; if (isLeft(result)) { - return result.left; + throw result.left; } else { - throw result.right; + return result.right; } }; @@ -100,9 +100,9 @@ export class RuleDataClient implements IRuleDataClient { const waitUntilReady = async () => { const result = await this.options.waitUntilReadyForWriting; if (isLeft(result)) { - return result.left; + throw result.left; } else { - throw result.right; + return result.right; } }; diff --git a/x-pack/plugins/rule_registry/server/rule_data_plugin_service/rule_data_plugin_service.ts b/x-pack/plugins/rule_registry/server/rule_data_plugin_service/rule_data_plugin_service.ts index d50154cb2d17f..b95effd4801b9 100644 --- a/x-pack/plugins/rule_registry/server/rule_data_plugin_service/rule_data_plugin_service.ts +++ b/x-pack/plugins/rule_registry/server/rule_data_plugin_service/rule_data_plugin_service.ts @@ -5,7 +5,7 @@ * 2.0. */ -import { left, right } from 'fp-ts/lib/Either'; +import { Either, isLeft, left, right } from 'fp-ts/lib/Either'; import { ElasticsearchClient, Logger } from 'kibana/server'; @@ -27,7 +27,7 @@ interface ConstructorOptions { */ export class RuleDataPluginService { private readonly resourceInstaller: ResourceInstaller; - private installCommonResources: Promise<'ok' | Error>; + private installCommonResources: Promise>; private isInitialized: boolean; constructor(private readonly options: ConstructorOptions) { @@ -38,7 +38,7 @@ export class RuleDataPluginService { isWriteEnabled: options.isWriteEnabled, }); - this.installCommonResources = Promise.resolve('ok'); + this.installCommonResources = Promise.resolve(right('ok')); this.isInitialized = false; } @@ -77,14 +77,12 @@ export class RuleDataPluginService { */ public initializeService(): void { // Run the installation of common resources and handle exceptions. - // NOTE: this promise cannot reject, otherwise it will lead to an - // unhandled promise rejection shutting down Kibana process. this.installCommonResources = this.resourceInstaller .installCommonResources() - .then(() => 'ok') + .then(() => right('ok' as const)) .catch((e) => { this.options.logger.error(e); - return e; // propagates it to the index initialization phase + return left(e); // propagates it to the index initialization phase }); this.isInitialized = true; @@ -110,27 +108,27 @@ export class RuleDataPluginService { const waitUntilClusterClientAvailable = async (): Promise => { try { const clusterClient = await this.options.getClusterClient(); - return left(clusterClient); + return right(clusterClient); } catch (e) { this.options.logger.error(e); - return right(e); + return left(e); } }; const waitUntilIndexResourcesInstalled = async (): Promise => { try { const result = await this.installCommonResources; - if (result !== 'ok') { - return right(result); + if (isLeft(result)) { + return result; } await this.resourceInstaller.installIndexLevelResources(indexInfo); const clusterClient = await this.options.getClusterClient(); - return left(clusterClient); + return right(clusterClient); } catch (e) { this.options.logger.error(e); - return right(e); + return left(e); } };