Add try...catch in DB index creation
MODIFY
src/authorization/Authorizations.ts
src/authorization/Authorizations.ts
@@ -30,7 +30,6 @@ import Tag from '../types/Tag';
import TagStorage from '../storage/mongodb/TagStorage';
import Tenant from '../types/Tenant';
import TenantComponents from '../types/TenantComponents';
-import TenantStorage from '../storage/mongodb/TenantStorage';
import Transaction from '../types/Transaction';
import TransactionStorage from '../storage/mongodb/TransactionStorage';
import UserStorage from '../storage/mongodb/UserStorage';

MODIFY
src/storage/mongodb/MongoDBStorage.ts
src/storage/mongodb/MongoDBStorage.ts
@@ -334,6 +334,7 @@ export default class MongoDBStorage {
action: ServerAction.MONGO_DB
});
}
+ try {
// Get all the collections
const currentCollections = await this.db.listCollections().toArray();
const tenantCollectionName = DatabaseUtils.getCollectionName(tenantID, name);
@@ -343,7 +344,15 @@ export default class MongoDBStorage {
try {
await this.db.createCollection(tenantCollectionName);
} catch (error) {
- console.error(chalk.red(`Error in creating collection '${tenantCollectionName}': ${error.message as string}`));
+ const message = `Error in creating collection '${tenantID}.${tenantCollectionName}': ${error.message as string}`;
+ console.error(chalk.red(message));
+ await Logging.logError({
+ tenantID: Constants.DEFAULT_TENANT,
+ action: ServerAction.MONGO_DB,
+ module: MODULE_NAME, method: 'handleIndexesInCollection',
+ message,
+ detailedMessages: { error: error.stack, tenantCollectionName, name, indexes }
+ });
}
}
// Indexes?
@@ -366,14 +375,28 @@ export default class MongoDBStorage {
// Delete the index
if (!foundIndex) {
if (Utils.isDevelopmentEnv()) {
- // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
- console.log(`Drop index ${databaseIndex.name} in collection ${tenantID}.${name}`);
+ const message = `Drop index '${databaseIndex.name}' in collection ${tenantCollectionName}`;
+ console.log(message);
+ await Logging.logInfo({
+ tenantID: Constants.DEFAULT_TENANT,
+ action: ServerAction.MONGO_DB,
+ module: MODULE_NAME, method: 'handleIndexesInCollection',
+ message,
+ detailedMessages: { tenantCollectionName, indexes, indexName: databaseIndex.name }
+ });
}
try {
await this.db.collection(tenantCollectionName).dropIndex(databaseIndex.key);
} catch (error) {
- // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
- console.error(chalk.red(`Error in dropping index '${databaseIndex.name}' in '${tenantCollectionName}': ${error.message}`));
+ const message = `Error in dropping index '${databaseIndex.name}' in '${tenantCollectionName}': ${error.message}`;
+ console.error(chalk.red(message));
+ await Logging.logError({
+ tenantID: Constants.DEFAULT_TENANT,
+ action: ServerAction.MONGO_DB,
+ module: MODULE_NAME, method: 'handleIndexesInCollection',
+ message,
+ detailedMessages: { error: error.stack, tenantCollectionName, name, indexes, indexName: databaseIndex.name }
+ });
}
}
}
@@ -385,16 +408,42 @@ export default class MongoDBStorage {
// Create the index
if (!foundDatabaseIndex) {
if (Utils.isDevelopmentEnv()) {
- console.log(`Create index ${JSON.stringify(index)} on collection ${tenantID}.${name}`);
+ const message = `Create index ${JSON.stringify(index)} in collection ${tenantCollectionName}`;
+ console.log(message);
+ await Logging.logInfo({
+ tenantID: Constants.DEFAULT_TENANT,
+ action: ServerAction.MONGO_DB,
+ module: MODULE_NAME, method: 'handleIndexesInCollection',
+ message,
+ detailedMessages: { tenantCollectionName, name, indexes, indexFields: index.fields, indexOptions: index.options }
+ });
}
try {
- // eslint-disable-next-line @typescript-eslint/await-thenable
await this.db.collection(tenantCollectionName).createIndex(index.fields, index.options);
} catch (error) {
- console.error(chalk.red(`Error in creating index '${JSON.stringify(index.fields)}' with options '${JSON.stringify(index.options)}' in '${tenantCollectionName}': ${error.message as string}`));
+ const message = `Error in creating index '${JSON.stringify(index.fields)}' with options '${JSON.stringify(index.options)}' in '${tenantCollectionName}': ${error.message as string}`;
+ console.error(chalk.red(message));
+ await Logging.logError({
+ tenantID: Constants.DEFAULT_TENANT,
+ action: ServerAction.MONGO_DB,
+ module: MODULE_NAME, method: 'handleIndexesInCollection',
+ message,
+ detailedMessages: { error: error.stack, tenantCollectionName, name, indexes, indexFields: index.fields, indexOptions: index.options }
+ });
+ }
}
}
}
+ } catch (error) {
+ const message = `Unexpected error in handling Collection '${tenantID}.${name}': ${error.message as string}`;
+ console.error(chalk.red(message));
+ await Logging.logError({
+ tenantID: Constants.DEFAULT_TENANT,
+ action: ServerAction.MONGO_DB,
+ module: MODULE_NAME, method: 'handleIndexesInCollection',
+ message,
+ detailedMessages: { error: error.stack, tenantID, name, indexes }
+ });
}
}

Add try...catch in DB index creation