fix: bot no longer crashes container on invalid token
- bot.js: 5 retries with 5s delay on init, graceful fallback to null - errorHandler.js: 5 retries on 404 (invalid token), stops polling after max retries but keeps process alive for admin panel - config.js: BOT_TOKEN missing logs warning instead of process.exit - index.js: bot handlers only registered when bot is available, admin panel always starts regardless of bot status
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
import logger from './logger.js';
|
||||
|
||||
let pollingRetries = 0;
|
||||
const MAX_POLLING_RETRIES = 5;
|
||||
|
||||
export default class ErrorHandler {
|
||||
static async handleError(bot, chatId, error, context) {
|
||||
logger.error({ err: error, context }, 'Error in handler');
|
||||
@@ -8,6 +11,7 @@ export default class ErrorHandler {
|
||||
? `Error: ${error.message}`
|
||||
: 'An error occurred. Please try again later.';
|
||||
|
||||
if (!bot) return;
|
||||
try {
|
||||
await bot.sendMessage(chatId, errorMessage);
|
||||
} catch (sendError) {
|
||||
@@ -17,8 +21,32 @@ export default class ErrorHandler {
|
||||
|
||||
static handlePollingError(error) {
|
||||
if (error.code === 'ETELEGRAM') {
|
||||
const statusCode = error.response?.statusCode;
|
||||
const description = error.response?.body?.description || '';
|
||||
|
||||
if (statusCode === 404 || description.includes('Not Found')) {
|
||||
pollingRetries++;
|
||||
logger.warn({
|
||||
attempt: pollingRetries,
|
||||
maxRetries: MAX_POLLING_RETRIES,
|
||||
statusCode,
|
||||
description
|
||||
}, 'Invalid bot token (404). Will retry up to %d times.', MAX_POLLING_RETRIES);
|
||||
|
||||
if (pollingRetries >= MAX_POLLING_RETRIES) {
|
||||
logger.error({ maxRetries: MAX_POLLING_RETRIES },
|
||||
'Bot token is invalid after %d retries. Stopping polling. Admin panel continues running.',
|
||||
MAX_POLLING_RETRIES);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (statusCode === 401 || statusCode === 403) {
|
||||
logger.error({ statusCode }, 'Authentication error. Stopping polling. Admin panel continues running.');
|
||||
return;
|
||||
}
|
||||
|
||||
logger.error({ err: error }, 'Telegram API Error');
|
||||
process.exit(1);
|
||||
} else {
|
||||
logger.error({ err: error }, 'Polling error');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user