From 78b2dda570c12dabac5f4eb32ec293e17af9a118 Mon Sep 17 00:00:00 2001 From: Struchkov Mark Date: Sun, 3 Jul 2022 22:02:15 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20Er?= =?UTF-8?q?rorHandler?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bot-context/pom.xml | 2 +- bot-core/pom.xml | 2 +- .../godfather/core/GeneralAutoResponder.java | 28 +++++++++++++++---- .../godfather/core/service/ErrorHandler.java | 18 ++++++++++++ pom.xml | 5 ++-- 5 files changed, 45 insertions(+), 10 deletions(-) create mode 100644 bot-core/src/main/java/dev/struchkov/godfather/core/service/ErrorHandler.java diff --git a/bot-context/pom.xml b/bot-context/pom.xml index 24a18f5..7b312c6 100644 --- a/bot-context/pom.xml +++ b/bot-context/pom.xml @@ -6,7 +6,7 @@ dev.struchkov.godfather godfather-bot - 0.0.7 + 0.0.8 bot-context diff --git a/bot-core/pom.xml b/bot-core/pom.xml index 5e4db41..124c8e8 100644 --- a/bot-core/pom.xml +++ b/bot-core/pom.xml @@ -6,7 +6,7 @@ dev.struchkov.godfather godfather-bot - 0.0.7 + 0.0.8 bot-core diff --git a/bot-core/src/main/java/dev/struchkov/godfather/core/GeneralAutoResponder.java b/bot-core/src/main/java/dev/struchkov/godfather/core/GeneralAutoResponder.java index 508f40a..8115559 100644 --- a/bot-core/src/main/java/dev/struchkov/godfather/core/GeneralAutoResponder.java +++ b/bot-core/src/main/java/dev/struchkov/godfather/core/GeneralAutoResponder.java @@ -12,6 +12,7 @@ import dev.struchkov.godfather.context.service.sender.Sending; import dev.struchkov.godfather.core.domain.unit.MainUnit; import dev.struchkov.godfather.core.domain.unit.UnitActiveType; import dev.struchkov.godfather.core.service.Accessibility; +import dev.struchkov.godfather.core.service.ErrorHandler; import dev.struchkov.godfather.core.service.action.ActionUnit; import dev.struchkov.godfather.core.service.action.AnswerCheckAction; import dev.struchkov.godfather.core.service.action.AnswerProcessingAction; @@ -32,6 +33,7 @@ import java.util.stream.Collectors; public class GeneralAutoResponder { + private ErrorHandler errorHandler; private final PersonSettingService personSettingService; private final UnitPointerService unitPointerService; private final StoryLine storyLine; @@ -78,6 +80,13 @@ public class GeneralAutoResponder { actionUnitMap.put(TypeUnit.TIMER, new AnswerTimerAction(timerService, this)); } + /** + * Позволяет установить перехватчик и обработчик исключений, возникающих при обработке юнитов. + */ + public void setErrorHandler(ErrorHandler errorHandler) { + this.errorHandler = errorHandler; + } + public void processingNewMessage(T newMessage) { if (newMessage != null) { final boolean state = personSettingService.getStateProcessingByPersonId(newMessage.getPersonId()).orElse(true); @@ -123,11 +132,20 @@ public class GeneralAutoResponder { } public void answer(T message, MainUnit unitAnswer) { - unitAnswer = getAction(message, unitAnswer); - unitAnswer = activeUnitAfter(unitAnswer, message); - final Optional optDefaultUnit = storyLine.getDefaultUnit(); - if (optDefaultUnit.isEmpty() || !optDefaultUnit.get().equals(unitAnswer)) { - unitPointerService.save(new UnitPointer(message.getPersonId(), unitAnswer.getName())); + try { + unitAnswer = getAction(message, unitAnswer); + unitAnswer = activeUnitAfter(unitAnswer, message); + + final Optional optDefaultUnit = storyLine.getDefaultUnit(); + if (optDefaultUnit.isEmpty() || !optDefaultUnit.get().equals(unitAnswer)) { + unitPointerService.save(new UnitPointer(message.getPersonId(), unitAnswer.getName())); + } + } catch (Exception e) { + if (errorHandler != null) { + errorHandler.handle(message, e); + } else { + throw e; + } } } diff --git a/bot-core/src/main/java/dev/struchkov/godfather/core/service/ErrorHandler.java b/bot-core/src/main/java/dev/struchkov/godfather/core/service/ErrorHandler.java new file mode 100644 index 0000000..1c20c19 --- /dev/null +++ b/bot-core/src/main/java/dev/struchkov/godfather/core/service/ErrorHandler.java @@ -0,0 +1,18 @@ +package dev.struchkov.godfather.core.service; + +import dev.struchkov.godfather.context.domain.content.Message; + +/** + * Используется для перехвата исключений, которые возникают при обработке юнитов. + */ +public interface ErrorHandler { + + /** + * Метод, который должен как-то обработать исключение. + * + * @param message Сообщение, после которого возникло исключение. + * @param e Объект исключения. + */ + void handle(Message message, Exception e); + +} diff --git a/pom.xml b/pom.xml index 206db28..34ab1fb 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ dev.struchkov.godfather godfather-bot - 0.0.7 + 0.0.8 pom @@ -32,7 +32,7 @@ UTF-8 UTF-8 - 0.0.7 + 0.0.8 ${godfather.ver} ${godfather.ver} @@ -60,7 +60,6 @@ ${godfather.core.ver} - dev.struchkov autoresponder