Files
telegram-shop/src/utils/errorHandler.js
NW 6aa7980ddf 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
2026-06-24 15:05:44 +01:00

54 lines
1.7 KiB
JavaScript

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');
const errorMessage = process.env.NODE_ENV === 'development'
? `Error: ${error.message}`
: 'An error occurred. Please try again later.';
if (!bot) return;
try {
await bot.sendMessage(chatId, errorMessage);
} catch (sendError) {
logger.error({ err: sendError }, 'Error sending error message');
}
}
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');
} else {
logger.error({ err: error }, 'Polling error');
}
}
}