From 8a1a28b26e6677d3cf52f0986b72292255eb5a9d Mon Sep 17 00:00:00 2001 From: Boki Date: Mon, 23 Jun 2025 12:35:10 -0400 Subject: [PATCH] added proper error messaged --- .env | 2 +- apps/stock/data-pipeline/src/index.ts | 35 ++++---- libs/core/logger/src/logger.ts | 6 +- libs/data/mongodb/src/client.ts | 119 +++++++++++++++----------- 4 files changed, 90 insertions(+), 72 deletions(-) diff --git a/.env b/.env index 8923e13..a029ae7 100644 --- a/.env +++ b/.env @@ -5,7 +5,7 @@ # Core Application Settings NODE_ENV=development LOG_LEVEL=trace -LOG_HIDE_OBJECT=false +LOG_HIDE_OBJECT=true # Data Service Configuration DATA_SERVICE_PORT=2001 diff --git a/apps/stock/data-pipeline/src/index.ts b/apps/stock/data-pipeline/src/index.ts index 9b5f9db..4ba3ed1 100644 --- a/apps/stock/data-pipeline/src/index.ts +++ b/apps/stock/data-pipeline/src/index.ts @@ -4,11 +4,7 @@ */ import { initializeStockConfig } from '@stock-bot/stock-config'; -import { - ServiceApplication, - createServiceContainerFromConfig, - initializeServices as initializeAwilixServices, -} from '@stock-bot/di'; +import { ServiceApplication } from '@stock-bot/di'; import { getLogger } from '@stock-bot/logger'; // Local imports @@ -58,17 +54,24 @@ const app = new ServiceApplication( // Container factory function async function createContainer(config: any) { - const container = createServiceContainerFromConfig(config, { - enableQuestDB: config.database.questdb?.enabled || false, - // Data pipeline needs all databases - enableMongoDB: true, - enablePostgres: true, - enableCache: true, - enableQueue: true, - enableBrowser: false, // Data pipeline doesn't need browser - enableProxy: false, // Data pipeline doesn't need proxy - }); - await initializeAwilixServices(container); + const { ServiceContainerBuilder } = await import('@stock-bot/di'); + const builder = new ServiceContainerBuilder(); + + const container = await builder + .withConfig(config) + .withOptions({ + enableQuestDB: false, // Disabled for now due to auth issues + // Data pipeline needs all databases + enableMongoDB: true, + enablePostgres: true, + enableCache: true, + enableQueue: true, + enableBrowser: false, // Data pipeline doesn't need browser + enableProxy: false, // Data pipeline doesn't need proxy + skipInitialization: false, // Let builder handle initialization + }) + .build(); + return container; } diff --git a/libs/core/logger/src/logger.ts b/libs/core/logger/src/logger.ts index 4ac1c4e..58b8c7b 100644 --- a/libs/core/logger/src/logger.ts +++ b/libs/core/logger/src/logger.ts @@ -177,15 +177,15 @@ export class Logger { let data = { ...this.context, ...metadata }; - // Hide all metadata if hideObject is enabled - if (globalConfig.hideObject) { + // Hide all metadata if hideObject is enabled, EXCEPT for error and fatal levels + if (globalConfig.hideObject && level !== 'error' && level !== 'fatal') { data = {}; // Clear all metadata } if (typeof message === 'string') { (this.pino as any)[level](data, message); } else { - if (globalConfig.hideObject) { + if (globalConfig.hideObject && level !== 'error' && level !== 'fatal') { (this.pino as any)[level]({}, `Object logged (hidden)`); } else { (this.pino as any)[level]({ ...data, data: message }, 'Object logged'); diff --git a/libs/data/mongodb/src/client.ts b/libs/data/mongodb/src/client.ts index 38176ab..251b6b7 100644 --- a/libs/data/mongodb/src/client.ts +++ b/libs/data/mongodb/src/client.ts @@ -45,69 +45,84 @@ export class MongoDBClient { /** * Connect to MongoDB with simple configuration */ - async connect(): Promise { + async connect(retryAttempts: number = 3, retryDelay: number = 1000): Promise { if (this.isConnected && this.client) { return; } - try { - const uri = this.buildConnectionUri(); - this.logger.info('Connecting to MongoDB...'); + let lastError: Error | null = null; - this.client = new MongoClient(uri, { - maxPoolSize: this.config.poolSettings?.maxPoolSize || 10, - minPoolSize: this.config.poolSettings?.minPoolSize || 1, - connectTimeoutMS: this.config.timeouts?.connectTimeout || 10000, - socketTimeoutMS: this.config.timeouts?.socketTimeout || 30000, - serverSelectionTimeoutMS: this.config.timeouts?.serverSelectionTimeout || 5000, - }); + for (let attempt = 1; attempt <= retryAttempts; attempt++) { + try { + const uri = this.buildConnectionUri(); + this.logger.info(`Connecting to MongoDB (attempt ${attempt}/${retryAttempts})...`); - await this.client.connect(); - await this.client.db(this.defaultDatabase).admin().ping(); + this.client = new MongoClient(uri, { + maxPoolSize: this.config.poolSettings?.maxPoolSize || 10, + minPoolSize: this.config.poolSettings?.minPoolSize || 1, + connectTimeoutMS: this.config.timeouts?.connectTimeout || 10000, + socketTimeoutMS: this.config.timeouts?.socketTimeout || 30000, + serverSelectionTimeoutMS: this.config.timeouts?.serverSelectionTimeout || 5000, + }); - // Set default database from config - this.db = this.client.db(this.defaultDatabase); - this.isConnected = true; + await this.client.connect(); + await this.client.db(this.defaultDatabase).admin().ping(); - // Update metrics - this.metrics.totalConnections = this.config.poolSettings?.maxPoolSize || 10; - this.metrics.idleConnections = this.metrics.totalConnections; + // Set default database from config + this.db = this.client.db(this.defaultDatabase); + this.isConnected = true; - // Fire connection event - if (this.events?.onConnect) { - await Promise.resolve(this.events.onConnect()); + // Update metrics + this.metrics.totalConnections = this.config.poolSettings?.maxPoolSize || 10; + this.metrics.idleConnections = this.metrics.totalConnections; + + // Fire connection event + if (this.events?.onConnect) { + await Promise.resolve(this.events.onConnect()); + } + + // Fire pool created event + if (this.events?.onPoolCreated) { + await Promise.resolve(this.events.onPoolCreated()); + } + + this.logger.info('Successfully connected to MongoDB', { + database: this.defaultDatabase, + poolSize: this.metrics.totalConnections, + }); + + // Start pool monitoring if dynamic sizing is enabled + if (this.dynamicPoolConfig?.enabled) { + this.startPoolMonitoring(); + } + + return; + } catch (error) { + lastError = error as Error; + this.metrics.errors++; + this.metrics.lastError = lastError.message; + + // Fire error event + if (this.events?.onError) { + await Promise.resolve(this.events.onError(lastError)); + } + + this.logger.error(`MongoDB connection attempt ${attempt} failed:`, error); + + if (this.client) { + await this.client.close(); + this.client = null; + } + + if (attempt < retryAttempts) { + await new Promise(resolve => setTimeout(resolve, retryDelay * attempt)); + } } - - // Fire pool created event - if (this.events?.onPoolCreated) { - await Promise.resolve(this.events.onPoolCreated()); - } - - this.logger.info('Successfully connected to MongoDB', { - database: this.defaultDatabase, - poolSize: this.metrics.totalConnections, - }); - - // Start pool monitoring if dynamic sizing is enabled - if (this.dynamicPoolConfig?.enabled) { - this.startPoolMonitoring(); - } - } catch (error) { - this.metrics.errors++; - this.metrics.lastError = error instanceof Error ? error.message : 'Unknown error'; - - // Fire error event - if (this.events?.onError) { - await Promise.resolve(this.events.onError(error as Error)); - } - - this.logger.error('MongoDB connection failed:', error); - if (this.client) { - await this.client.close(); - this.client = null; - } - throw error; } + + throw new Error( + `Failed to connect to MongoDB after ${retryAttempts} attempts: ${lastError?.message}` + ); } /**