Ensure the OCPP message sending stop if the WS is not open
[e-mobility-charging-stations-simulator.git] / src / performance / storage / MongoDBStorage.ts
index 851df0a1ba8248a582153056ce1bad688fd37d92..5a3783818cba77bcb16bf25e622ae81015f77fe8 100644 (file)
@@ -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<void> {
     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<void> {
     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`);
     }