From 158d957eee4c66bbf49c3dd74dbda8ca423804e9 Mon Sep 17 00:00:00 2001 From: Sheldan <5037282+Sheldan@users.noreply.github.com> Date: Tue, 26 May 2020 00:51:35 +0200 Subject: [PATCH] added exception handling (with templated messages) for setup of post target and system config --- .../NoChannelProvidedException.java | 20 +++++++++++++ .../core/interactive/PostTargetSetupStep.java | 5 ++-- .../interactive/PostTargetStepException.java | 30 +++++++++++++++++++ .../interactive/SystemConfigSetupStep.java | 2 +- .../SystemConfigStepException.java | 27 +++++++++++++++++ .../core/service/SetupServiceBean.java | 15 ++++++++++ ...up_no_channel_provided_exception_en_US.ftl | 1 + .../setup_post_target_exception_en_US.ftl | 1 + .../setup_system_config_exception_en_US.ftl | 1 + ..._channel_provided_exception_text_en_US.ftl | 1 + ...setup_post_target_exception_text_en_US.ftl | 1 + ...tup_system_config_exception_text_en_US.ftl | 1 + 12 files changed, 101 insertions(+), 4 deletions(-) create mode 100644 abstracto-application/core/core-impl/src/main/java/dev/sheldan/abstracto/core/interactive/NoChannelProvidedException.java create mode 100644 abstracto-application/core/core-impl/src/main/java/dev/sheldan/abstracto/core/interactive/PostTargetStepException.java create mode 100644 abstracto-application/core/core-impl/src/main/java/dev/sheldan/abstracto/core/interactive/SystemConfigStepException.java create mode 100644 abstracto-application/core/core-impl/src/main/resources/seedData/templates/exceptions/setup_no_channel_provided_exception_en_US.ftl create mode 100644 abstracto-application/core/core-impl/src/main/resources/seedData/templates/exceptions/setup_post_target_exception_en_US.ftl create mode 100644 abstracto-application/core/core-impl/src/main/resources/seedData/templates/exceptions/setup_system_config_exception_en_US.ftl create mode 100644 abstracto-application/template-config/src/main/resources/templates/en_US/core/exception/setup_no_channel_provided_exception_text_en_US.ftl create mode 100644 abstracto-application/template-config/src/main/resources/templates/en_US/core/exception/setup_post_target_exception_text_en_US.ftl create mode 100644 abstracto-application/template-config/src/main/resources/templates/en_US/core/exception/setup_system_config_exception_text_en_US.ftl diff --git a/abstracto-application/core/core-impl/src/main/java/dev/sheldan/abstracto/core/interactive/NoChannelProvidedException.java b/abstracto-application/core/core-impl/src/main/java/dev/sheldan/abstracto/core/interactive/NoChannelProvidedException.java new file mode 100644 index 000000000..1aa369375 --- /dev/null +++ b/abstracto-application/core/core-impl/src/main/java/dev/sheldan/abstracto/core/interactive/NoChannelProvidedException.java @@ -0,0 +1,20 @@ +package dev.sheldan.abstracto.core.interactive; + +import dev.sheldan.abstracto.core.exception.AbstractoRunTimeException; +import dev.sheldan.abstracto.templating.Templatable; + +public class NoChannelProvidedException extends AbstractoRunTimeException implements Templatable { + public NoChannelProvidedException(String message) { + super(message); + } + + @Override + public String getTemplateName() { + return "setup_no_channel_provided_exception"; + } + + @Override + public Object getTemplateModel() { + return new Object(); + } +} diff --git a/abstracto-application/core/core-impl/src/main/java/dev/sheldan/abstracto/core/interactive/PostTargetSetupStep.java b/abstracto-application/core/core-impl/src/main/java/dev/sheldan/abstracto/core/interactive/PostTargetSetupStep.java index ca170e962..d7365bc70 100644 --- a/abstracto-application/core/core-impl/src/main/java/dev/sheldan/abstracto/core/interactive/PostTargetSetupStep.java +++ b/abstracto-application/core/core-impl/src/main/java/dev/sheldan/abstracto/core/interactive/PostTargetSetupStep.java @@ -84,8 +84,7 @@ public class PostTargetSetupStep extends AbstractConfigSetupStep { result = SetupStepResult.fromCancelled(); } else { if(message.getMentionedChannels().size() == 0) { - future.completeExceptionally(new RuntimeException()); - return; + throw new NoChannelProvidedException("No channel was provided."); } TextChannel textChannel = message.getMentionedChannels().get(0); PostTargetDelayedActionConfig build = PostTargetDelayedActionConfig @@ -106,7 +105,7 @@ public class PostTargetSetupStep extends AbstractConfigSetupStep { future.complete(result); } catch (Exception e) { log.error("Failed to handle post target step.", e); - future.completeExceptionally(e); + future.completeExceptionally(new PostTargetStepException(e)); } }; interactiveService.createMessageWithResponse(messageText, aUserInAServer, channel.get(), parameter.getPreviousMessageId(), configAction, finalAction); diff --git a/abstracto-application/core/core-impl/src/main/java/dev/sheldan/abstracto/core/interactive/PostTargetStepException.java b/abstracto-application/core/core-impl/src/main/java/dev/sheldan/abstracto/core/interactive/PostTargetStepException.java new file mode 100644 index 000000000..5729d4c44 --- /dev/null +++ b/abstracto-application/core/core-impl/src/main/java/dev/sheldan/abstracto/core/interactive/PostTargetStepException.java @@ -0,0 +1,30 @@ +package dev.sheldan.abstracto.core.interactive; + +import dev.sheldan.abstracto.core.exception.AbstractoRunTimeException; +import dev.sheldan.abstracto.templating.Templatable; + +import java.util.HashMap; + +public class PostTargetStepException extends AbstractoRunTimeException implements Templatable { + public PostTargetStepException(Throwable cause) { + super("", cause); + } + + @Override + public String getTemplateName() { + return "setup_post_target_exception"; + } + + @Override + public Object getTemplateModel() { + HashMap stringStringHashMap = new HashMap<>(); + if(getCause() instanceof Templatable) { + Templatable templatable = (Templatable) getCause(); + stringStringHashMap.put("templateKey", templatable.getTemplateName()); + stringStringHashMap.put("templateModel", templatable.getTemplateModel()); + } + stringStringHashMap.put("message", getCause().getMessage()); + + return stringStringHashMap; + } +} diff --git a/abstracto-application/core/core-impl/src/main/java/dev/sheldan/abstracto/core/interactive/SystemConfigSetupStep.java b/abstracto-application/core/core-impl/src/main/java/dev/sheldan/abstracto/core/interactive/SystemConfigSetupStep.java index 24abc226b..2b8029f3f 100644 --- a/abstracto-application/core/core-impl/src/main/java/dev/sheldan/abstracto/core/interactive/SystemConfigSetupStep.java +++ b/abstracto-application/core/core-impl/src/main/java/dev/sheldan/abstracto/core/interactive/SystemConfigSetupStep.java @@ -95,7 +95,7 @@ public class SystemConfigSetupStep extends AbstractConfigSetupStep { future.complete(result); } catch (Exception e) { log.warn("Failed to handle system config. Retrying..", e); - future.completeExceptionally(e); + future.completeExceptionally(new SystemConfigStepException(e)); } }; interactiveService.createMessageWithResponse(messageText, aUserInAServer, channel.get(), parameter.getPreviousMessageId(), configAction, finalAction); diff --git a/abstracto-application/core/core-impl/src/main/java/dev/sheldan/abstracto/core/interactive/SystemConfigStepException.java b/abstracto-application/core/core-impl/src/main/java/dev/sheldan/abstracto/core/interactive/SystemConfigStepException.java new file mode 100644 index 000000000..17cf41263 --- /dev/null +++ b/abstracto-application/core/core-impl/src/main/java/dev/sheldan/abstracto/core/interactive/SystemConfigStepException.java @@ -0,0 +1,27 @@ +package dev.sheldan.abstracto.core.interactive; + +import dev.sheldan.abstracto.core.exception.AbstractoRunTimeException; +import dev.sheldan.abstracto.templating.Templatable; + +import java.util.HashMap; + +public class SystemConfigStepException extends AbstractoRunTimeException implements Templatable { + + + public SystemConfigStepException(Throwable cause) { + super("", cause); + } + + @Override + public String getTemplateName() { + return "setup_system_config_exception"; + } + + @Override + public Object getTemplateModel() { + HashMap stringStringHashMap = new HashMap<>(); + stringStringHashMap.put("message", getCause().getMessage()); + + return stringStringHashMap; + } +} diff --git a/abstracto-application/core/core-impl/src/main/java/dev/sheldan/abstracto/core/service/SetupServiceBean.java b/abstracto-application/core/core-impl/src/main/java/dev/sheldan/abstracto/core/service/SetupServiceBean.java index 10856b86b..ba19de73c 100644 --- a/abstracto-application/core/core-impl/src/main/java/dev/sheldan/abstracto/core/service/SetupServiceBean.java +++ b/abstracto-application/core/core-impl/src/main/java/dev/sheldan/abstracto/core/service/SetupServiceBean.java @@ -6,6 +6,7 @@ import dev.sheldan.abstracto.core.interactive.*; import dev.sheldan.abstracto.core.models.AServerChannelUserId; import dev.sheldan.abstracto.core.models.template.commands.SetupCompletedNotificationModel; import dev.sheldan.abstracto.core.models.template.commands.SetupInitialMessageModel; +import dev.sheldan.abstracto.templating.Templatable; import dev.sheldan.abstracto.templating.service.TemplateService; import lombok.extern.slf4j.Slf4j; import net.dv8tion.jda.api.entities.TextChannel; @@ -42,6 +43,9 @@ public class SetupServiceBean implements SetupService { @Autowired private TemplateService templateService; + @Autowired + private BotService botService; + @Override public void performSetup(FeatureConfig featureConfig, AServerChannelUserId user, Long initialMessageId) { List requiredSystemConfigKeys = featureConfig.getRequiredSystemConfigKeys(); @@ -110,11 +114,22 @@ public class SetupServiceBean implements SetupService { } }).exceptionally(throwable -> { + showExceptionMessage(throwable.getCause(), aUserInAServer); executeStep(aUserInAServer, execution, delayedActionConfigs, featureConfig); return null; }); } + @Transactional + public void showExceptionMessage(Throwable throwable, AServerChannelUserId aServerChannelUserId) { + if(throwable instanceof Templatable) { + Templatable exception = (Templatable) throwable; + String text = templateService.renderTemplate(exception.getTemplateName(), exception.getTemplateModel()); + Optional channelOptional = botService.getTextChannelFromServer(aServerChannelUserId.getGuildId(), aServerChannelUserId.getChannelId()); + channelOptional.ifPresent(channel -> channelService.sendTextToChannel(text, channel)); + } + } + @Transactional public void executePostSetupSteps(List delayedActionConfigs, AServerChannelUserId user, Long initialMessage, FeatureConfig featureConfig) { SetupSummaryStepParameter parameter = SetupSummaryStepParameter diff --git a/abstracto-application/core/core-impl/src/main/resources/seedData/templates/exceptions/setup_no_channel_provided_exception_en_US.ftl b/abstracto-application/core/core-impl/src/main/resources/seedData/templates/exceptions/setup_no_channel_provided_exception_en_US.ftl new file mode 100644 index 000000000..76b7ab8de --- /dev/null +++ b/abstracto-application/core/core-impl/src/main/resources/seedData/templates/exceptions/setup_no_channel_provided_exception_en_US.ftl @@ -0,0 +1 @@ +<#include "setup_no_channel_provided_exception_text"> \ No newline at end of file diff --git a/abstracto-application/core/core-impl/src/main/resources/seedData/templates/exceptions/setup_post_target_exception_en_US.ftl b/abstracto-application/core/core-impl/src/main/resources/seedData/templates/exceptions/setup_post_target_exception_en_US.ftl new file mode 100644 index 000000000..6cc08646c --- /dev/null +++ b/abstracto-application/core/core-impl/src/main/resources/seedData/templates/exceptions/setup_post_target_exception_en_US.ftl @@ -0,0 +1 @@ +<#assign text><#if templateKey?has_content><#assign exceptionModel=templateModel><#include "${templateKey}"><#else>${message}<#include "setup_post_target_exception_text"> \ No newline at end of file diff --git a/abstracto-application/core/core-impl/src/main/resources/seedData/templates/exceptions/setup_system_config_exception_en_US.ftl b/abstracto-application/core/core-impl/src/main/resources/seedData/templates/exceptions/setup_system_config_exception_en_US.ftl new file mode 100644 index 000000000..59ff3e53c --- /dev/null +++ b/abstracto-application/core/core-impl/src/main/resources/seedData/templates/exceptions/setup_system_config_exception_en_US.ftl @@ -0,0 +1 @@ +<#include "setup_system_config_exception_text"> \ No newline at end of file diff --git a/abstracto-application/template-config/src/main/resources/templates/en_US/core/exception/setup_no_channel_provided_exception_text_en_US.ftl b/abstracto-application/template-config/src/main/resources/templates/en_US/core/exception/setup_no_channel_provided_exception_text_en_US.ftl new file mode 100644 index 000000000..c5bf27580 --- /dev/null +++ b/abstracto-application/template-config/src/main/resources/templates/en_US/core/exception/setup_no_channel_provided_exception_text_en_US.ftl @@ -0,0 +1 @@ +No channel was entered. \ No newline at end of file diff --git a/abstracto-application/template-config/src/main/resources/templates/en_US/core/exception/setup_post_target_exception_text_en_US.ftl b/abstracto-application/template-config/src/main/resources/templates/en_US/core/exception/setup_post_target_exception_text_en_US.ftl new file mode 100644 index 000000000..f86c0e759 --- /dev/null +++ b/abstracto-application/template-config/src/main/resources/templates/en_US/core/exception/setup_post_target_exception_text_en_US.ftl @@ -0,0 +1 @@ +Failed to configure post target: ${text} \ No newline at end of file diff --git a/abstracto-application/template-config/src/main/resources/templates/en_US/core/exception/setup_system_config_exception_text_en_US.ftl b/abstracto-application/template-config/src/main/resources/templates/en_US/core/exception/setup_system_config_exception_text_en_US.ftl new file mode 100644 index 000000000..c98cde392 --- /dev/null +++ b/abstracto-application/template-config/src/main/resources/templates/en_US/core/exception/setup_system_config_exception_text_en_US.ftl @@ -0,0 +1 @@ +Failed to configure system config: ${message}. \ No newline at end of file