From a6ceb16a6b67653adaa0b40cd057826c71f44f36 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sun, 12 Sep 2021 14:01:42 +0200 Subject: [PATCH] Add error handling in one performance storage class MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/performance/storage/JSONFileStorage.ts | 6 ++++-- src/performance/storage/MikroORMStorage.ts | 19 ++++++++++++++++--- src/performance/storage/MongoDBStorage.ts | 9 ++++++--- 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/src/performance/storage/JSONFileStorage.ts b/src/performance/storage/JSONFileStorage.ts index cc7920a0..25ba2367 100644 --- a/src/performance/storage/JSONFileStorage.ts +++ b/src/performance/storage/JSONFileStorage.ts @@ -33,7 +33,9 @@ export class JSONFileStorage extends Storage { public open(): void { try { - this.fd = fs.openSync(this.dbName, 'a+'); + if (!this?.fd) { + this.fd = fs.openSync(this.dbName, 'a+'); + } } catch (error) { FileUtils.handleFileException(this.logPrefix, Constants.PERFORMANCE_RECORDS_FILETYPE, this.dbName, error); } @@ -41,7 +43,7 @@ export class JSONFileStorage extends Storage { public close(): void { try { - if (this.fd) { + if (this?.fd) { fs.closeSync(this.fd); this.fd = null; } diff --git a/src/performance/storage/MikroORMStorage.ts b/src/performance/storage/MikroORMStorage.ts index 96740a8f..c06617f6 100644 --- a/src/performance/storage/MikroORMStorage.ts +++ b/src/performance/storage/MikroORMStorage.ts @@ -12,7 +12,7 @@ import { TsMorphMetadataProvider } from '@mikro-orm/reflection'; export class MikroORMStorage extends Storage { private storageType: StorageType; - private orm: MikroORM; + private orm: MikroORM | null; constructor(storageURI: string, logPrefix: string, storageType: StorageType) { super(storageURI, logPrefix); @@ -30,11 +30,24 @@ export class MikroORMStorage extends Storage { } public async open(): Promise { - this.orm = await MikroORM.init(this.getOptions(), true); + try { + if (!this?.orm) { + this.orm = await MikroORM.init(this.getOptions(), true); + } + } catch (error) { + this.handleDBError(this.storageType, error); + } } public async close(): Promise { - await this.orm.close(); + try { + if (this?.orm) { + await this.orm.close(); + this.orm = null; + } + } catch (error) { + this.handleDBError(this.storageType, error); + } } private getDBName(): string { diff --git a/src/performance/storage/MongoDBStorage.ts b/src/performance/storage/MongoDBStorage.ts index 851df0a1..5a378381 100644 --- a/src/performance/storage/MongoDBStorage.ts +++ b/src/performance/storage/MongoDBStorage.ts @@ -7,7 +7,7 @@ import { Storage } from './Storage'; import { StorageType } from '../../types/Storage'; export class MongoDBStorage extends Storage { - private client: MongoClient; + private client: MongoClient | null; private connected: boolean; constructor(storageURI: string, logPrefix: string) { @@ -28,7 +28,7 @@ export class MongoDBStorage extends Storage { public async open(): Promise { try { - if (!this.connected) { + if (!this.connected && this?.client) { await this.client.connect(); this.connected = true; } @@ -39,7 +39,7 @@ export class MongoDBStorage extends Storage { public async close(): Promise { try { - if (this.connected) { + if (this.connected && this?.client) { await this.client.close(); this.connected = false; } @@ -49,6 +49,9 @@ export class MongoDBStorage extends Storage { } private checkDBConnection() { + if (!this?.client) { + throw new Error(`${this.logPrefix} ${this.getDBNameFromStorageType(StorageType.MONGO_DB)} client initialization failed while trying to issue a request`); + } if (!this.connected) { throw new Error(`${this.logPrefix} ${this.getDBNameFromStorageType(StorageType.MONGO_DB)} connection not opened while trying to issue a request`); } -- 2.34.1