From 022fb152f7ebe9f083540f75aa96c7031b947d41 Mon Sep 17 00:00:00 2001 From: Struchkov Mark Date: Wed, 15 Feb 2023 23:05:43 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20Er?= =?UTF-8?q?rorHandlerService?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../telegram/config/TelegramBotConfig.java | 3 ++ .../telegram/service/ErrorHandlerService.java | 28 +++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/service/ErrorHandlerService.java diff --git a/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/config/TelegramBotConfig.java b/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/config/TelegramBotConfig.java index b4e2538..ce60a58 100644 --- a/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/config/TelegramBotConfig.java +++ b/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/config/TelegramBotConfig.java @@ -10,6 +10,7 @@ import dev.struchkov.bot.gitlab.telegram.unit.command.EnableProjectNotify; import dev.struchkov.bot.gitlab.telegram.unit.flow.InitSettingFlow; import dev.struchkov.godfather.main.core.unit.TypeUnit; import dev.struchkov.godfather.main.domain.content.Mail; +import dev.struchkov.godfather.simple.context.service.ErrorHandler; import dev.struchkov.godfather.simple.context.service.EventHandler; import dev.struchkov.godfather.simple.context.service.PersonSettingService; import dev.struchkov.godfather.simple.context.service.UnitPointerService; @@ -99,6 +100,7 @@ public class TelegramBotConfig { @Qualifier("messageExecutorService") ExecutorService executorService, TelegramSending sending, PersonSettingService personSettingService, + ErrorHandler errorHandler, StorylineService storylineService ) { @@ -107,6 +109,7 @@ public class TelegramBotConfig { ); autoresponder.initActionUnit(TypeUnit.BACK_CMD, new RollBackCmdAction<>(storylineService)); autoresponder.setExecutorService(executorService); + autoresponder.setErrorHandler(errorHandler); return autoresponder; } diff --git a/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/service/ErrorHandlerService.java b/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/service/ErrorHandlerService.java new file mode 100644 index 0000000..8287aa2 --- /dev/null +++ b/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/service/ErrorHandlerService.java @@ -0,0 +1,28 @@ +package dev.struchkov.bot.gitlab.telegram.service; + +import dev.struchkov.godfather.main.domain.BoxAnswer; +import dev.struchkov.godfather.main.domain.content.Message; +import dev.struchkov.godfather.simple.context.service.ErrorHandler; +import dev.struchkov.godfather.telegram.simple.context.service.TelegramSending; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; + +import static dev.struchkov.godfather.main.domain.BoxAnswer.boxAnswer; + +@Slf4j +@Service +@RequiredArgsConstructor +public class ErrorHandlerService implements ErrorHandler { + + private final TelegramSending telegramSending; + + @Override + public void handle(Message message, Exception e) { + log.error(e.getMessage(), e); + final BoxAnswer boxAnswer = boxAnswer(e.getMessage()); + boxAnswer.setRecipientIfNull(message.getPersonId()); + telegramSending.send(boxAnswer); + } + +}