Добавил ErrorHandler

This commit is contained in:
Struchkov Mark 2022-07-03 22:02:15 +03:00
parent 7e4264a9e6
commit 78b2dda570
5 changed files with 45 additions and 10 deletions

View File

@ -6,7 +6,7 @@
<parent> <parent>
<groupId>dev.struchkov.godfather</groupId> <groupId>dev.struchkov.godfather</groupId>
<artifactId>godfather-bot</artifactId> <artifactId>godfather-bot</artifactId>
<version>0.0.7</version> <version>0.0.8</version>
</parent> </parent>
<artifactId>bot-context</artifactId> <artifactId>bot-context</artifactId>

View File

@ -6,7 +6,7 @@
<parent> <parent>
<groupId>dev.struchkov.godfather</groupId> <groupId>dev.struchkov.godfather</groupId>
<artifactId>godfather-bot</artifactId> <artifactId>godfather-bot</artifactId>
<version>0.0.7</version> <version>0.0.8</version>
</parent> </parent>
<artifactId>bot-core</artifactId> <artifactId>bot-core</artifactId>

View File

@ -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.MainUnit;
import dev.struchkov.godfather.core.domain.unit.UnitActiveType; import dev.struchkov.godfather.core.domain.unit.UnitActiveType;
import dev.struchkov.godfather.core.service.Accessibility; 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.ActionUnit;
import dev.struchkov.godfather.core.service.action.AnswerCheckAction; import dev.struchkov.godfather.core.service.action.AnswerCheckAction;
import dev.struchkov.godfather.core.service.action.AnswerProcessingAction; import dev.struchkov.godfather.core.service.action.AnswerProcessingAction;
@ -32,6 +33,7 @@ import java.util.stream.Collectors;
public class GeneralAutoResponder<T extends Message> { public class GeneralAutoResponder<T extends Message> {
private ErrorHandler errorHandler;
private final PersonSettingService personSettingService; private final PersonSettingService personSettingService;
private final UnitPointerService unitPointerService; private final UnitPointerService unitPointerService;
private final StoryLine storyLine; private final StoryLine storyLine;
@ -78,6 +80,13 @@ public class GeneralAutoResponder<T extends Message> {
actionUnitMap.put(TypeUnit.TIMER, new AnswerTimerAction(timerService, this)); actionUnitMap.put(TypeUnit.TIMER, new AnswerTimerAction(timerService, this));
} }
/**
* Позволяет установить перехватчик и обработчик исключений, возникающих при обработке юнитов.
*/
public void setErrorHandler(ErrorHandler errorHandler) {
this.errorHandler = errorHandler;
}
public void processingNewMessage(T newMessage) { public void processingNewMessage(T newMessage) {
if (newMessage != null) { if (newMessage != null) {
final boolean state = personSettingService.getStateProcessingByPersonId(newMessage.getPersonId()).orElse(true); final boolean state = personSettingService.getStateProcessingByPersonId(newMessage.getPersonId()).orElse(true);
@ -123,12 +132,21 @@ public class GeneralAutoResponder<T extends Message> {
} }
public void answer(T message, MainUnit unitAnswer) { public void answer(T message, MainUnit unitAnswer) {
try {
unitAnswer = getAction(message, unitAnswer); unitAnswer = getAction(message, unitAnswer);
unitAnswer = activeUnitAfter(unitAnswer, message); unitAnswer = activeUnitAfter(unitAnswer, message);
final Optional<MainUnit> optDefaultUnit = storyLine.getDefaultUnit(); final Optional<MainUnit> optDefaultUnit = storyLine.getDefaultUnit();
if (optDefaultUnit.isEmpty() || !optDefaultUnit.get().equals(unitAnswer)) { if (optDefaultUnit.isEmpty() || !optDefaultUnit.get().equals(unitAnswer)) {
unitPointerService.save(new UnitPointer(message.getPersonId(), unitAnswer.getName())); unitPointerService.save(new UnitPointer(message.getPersonId(), unitAnswer.getName()));
} }
} catch (Exception e) {
if (errorHandler != null) {
errorHandler.handle(message, e);
} else {
throw e;
}
}
} }
private MainUnit activeUnitAfter(MainUnit mainUnit, T content) { private MainUnit activeUnitAfter(MainUnit mainUnit, T content) {

View File

@ -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);
}

View File

@ -6,7 +6,7 @@
<groupId>dev.struchkov.godfather</groupId> <groupId>dev.struchkov.godfather</groupId>
<artifactId>godfather-bot</artifactId> <artifactId>godfather-bot</artifactId>
<version>0.0.7</version> <version>0.0.8</version>
<packaging>pom</packaging> <packaging>pom</packaging>
<modules> <modules>
@ -32,7 +32,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<godfather.ver>0.0.7</godfather.ver> <godfather.ver>0.0.8</godfather.ver>
<godfather.context.ver>${godfather.ver}</godfather.context.ver> <godfather.context.ver>${godfather.ver}</godfather.context.ver>
<godfather.core.ver>${godfather.ver}</godfather.core.ver> <godfather.core.ver>${godfather.ver}</godfather.core.ver>
@ -60,7 +60,6 @@
<version>${godfather.core.ver}</version> <version>${godfather.core.ver}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>dev.struchkov</groupId> <groupId>dev.struchkov</groupId>
<artifactId>autoresponder</artifactId> <artifactId>autoresponder</artifactId>