diff --git a/apps/bot/src/index.ts b/apps/bot/src/index.ts index 6b1fbac..1a321bb 100644 --- a/apps/bot/src/index.ts +++ b/apps/bot/src/index.ts @@ -6,10 +6,12 @@ import { logger } from '@repo/logger'; import { PrismaStorageAdapter } from './session-storage'; import { BotContext } from './types'; import { upsertUser } from './middlewares/upsert-user'; +import { onError } from './middlewares/on-error'; const bot = new Bot(config.BOT_TOKEN); const storage = new PrismaStorageAdapter(); +bot.use(onError); bot.use(session({ storage, initial: () => ({}) })); bot.use(upsertUser); diff --git a/apps/bot/src/middlewares/on-error.ts b/apps/bot/src/middlewares/on-error.ts new file mode 100644 index 0000000..6ba9a64 --- /dev/null +++ b/apps/bot/src/middlewares/on-error.ts @@ -0,0 +1,15 @@ +import { MiddlewareFn } from 'grammy'; +import { BotContext } from '../types'; +import { logger } from '@repo/logger'; + +const errorReply = + 'Извините, произошла ошибка при обработке вашего запроса. Пожалуйста, попробуйте позже.'; +const replyErrorLog = 'Не удалось отправить сообщение об ошибке пользователю'; + +export const onError: MiddlewareFn = async (ctx, next) => { + return await next().catch(async () => { + await ctx.reply(errorReply).catch((err) => { + logger.error(replyErrorLog, err); + }); + }); +};