🔧 fix(telegram-bot): improve database connection logging and error messages

- Add connection test on startup (SELECT 1)
- Log masked database URL on connection attempt
- Better error messages for /users command
This commit is contained in:
Till-JS 2026-01-26 11:14:52 +01:00
parent 79e3c09af2
commit 021c6e789e
2 changed files with 23 additions and 9 deletions

View file

@ -67,13 +67,21 @@ export class BotUpdate {
this.logger.log(`/users command from ${ctx.from?.id}`);
await ctx.reply('👥 Lade User-Statistiken...');
const stats = await this.usersService.getUserStats();
if (!stats) {
await ctx.reply('❌ Datenbank nicht verfügbar');
return;
}
try {
const stats = await this.usersService.getUserStats();
if (!stats) {
this.logger.warn('User stats returned null - database may not be configured');
await ctx.reply('❌ Datenbank nicht verfügbar. Prüfe DATABASE_URL Konfiguration.');
return;
}
const report = formatUsersReport(stats);
await ctx.replyWithHTML(report);
const report = formatUsersReport(stats);
await ctx.replyWithHTML(report);
} catch (error) {
this.logger.error('Failed to get user stats:', error);
await ctx.reply(
`❌ Fehler beim Laden der User-Statistiken: ${error instanceof Error ? error.message : 'Unbekannter Fehler'}`
);
}
}
}

View file

@ -31,10 +31,16 @@ export class UsersService implements OnModuleInit {
async onModuleInit() {
if (this.databaseUrl) {
try {
// Mask password in logs
const maskedUrl = this.databaseUrl.replace(/:([^@]+)@/, ':****@');
this.logger.log(`Connecting to database: ${maskedUrl}`);
this.sql = postgres(this.databaseUrl);
this.logger.log('Database connection initialized');
// Test connection
await this.sql`SELECT 1`;
this.logger.log('Database connection initialized and tested successfully');
} catch (error) {
this.logger.warn('Failed to initialize database connection:', error);
this.logger.error('Failed to initialize database connection:', error);
this.sql = null;
}
} else {
this.logger.warn('DATABASE_URL not configured, user stats will be unavailable');