added proper error messaged

This commit is contained in:
Boki 2025-06-23 12:35:10 -04:00
parent 71f771862b
commit 8a1a28b26e
4 changed files with 90 additions and 72 deletions

2
.env
View file

@ -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

View file

@ -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,8 +54,13 @@ const app = new ServiceApplication(
// Container factory function
async function createContainer(config: any) {
const container = createServiceContainerFromConfig(config, {
enableQuestDB: config.database.questdb?.enabled || false,
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,
@ -67,8 +68,10 @@ async function createContainer(config: any) {
enableQueue: true,
enableBrowser: false, // Data pipeline doesn't need browser
enableProxy: false, // Data pipeline doesn't need proxy
});
await initializeAwilixServices(container);
skipInitialization: false, // Let builder handle initialization
})
.build();
return container;
}

View file

@ -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');

View file

@ -45,14 +45,17 @@ export class MongoDBClient {
/**
* Connect to MongoDB with simple configuration
*/
async connect(): Promise<void> {
async connect(retryAttempts: number = 3, retryDelay: number = 1000): Promise<void> {
if (this.isConnected && this.client) {
return;
}
let lastError: Error | null = null;
for (let attempt = 1; attempt <= retryAttempts; attempt++) {
try {
const uri = this.buildConnectionUri();
this.logger.info('Connecting to MongoDB...');
this.logger.info(`Connecting to MongoDB (attempt ${attempt}/${retryAttempts})...`);
this.client = new MongoClient(uri, {
maxPoolSize: this.config.poolSettings?.maxPoolSize || 10,
@ -92,23 +95,35 @@ export class MongoDBClient {
if (this.dynamicPoolConfig?.enabled) {
this.startPoolMonitoring();
}
return;
} catch (error) {
lastError = error as Error;
this.metrics.errors++;
this.metrics.lastError = error instanceof Error ? error.message : 'Unknown error';
this.metrics.lastError = lastError.message;
// Fire error event
if (this.events?.onError) {
await Promise.resolve(this.events.onError(error as Error));
await Promise.resolve(this.events.onError(lastError));
}
this.logger.error('MongoDB connection failed:', error);
this.logger.error(`MongoDB connection attempt ${attempt} failed:`, error);
if (this.client) {
await this.client.close();
this.client = null;
}
throw error;
if (attempt < retryAttempts) {
await new Promise(resolve => setTimeout(resolve, retryDelay * attempt));
}
}
}
throw new Error(
`Failed to connect to MongoDB after ${retryAttempts} attempts: ${lastError?.message}`
);
}
/**
* Disconnect from MongoDB