added exception handling (with templated messages) for setup of post target and system config

This commit is contained in:
Sheldan
2020-05-26 00:51:35 +02:00
parent 7d18f51e08
commit 158d957eee
12 changed files with 101 additions and 4 deletions

View File

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

View File

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

View File

@@ -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<String, Object> 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;
}
}

View File

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

View File

@@ -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<String, String> stringStringHashMap = new HashMap<>();
stringStringHashMap.put("message", getCause().getMessage());
return stringStringHashMap;
}
}

View File

@@ -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<String> 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<TextChannel> channelOptional = botService.getTextChannelFromServer(aServerChannelUserId.getGuildId(), aServerChannelUserId.getChannelId());
channelOptional.ifPresent(channel -> channelService.sendTextToChannel(text, channel));
}
}
@Transactional
public void executePostSetupSteps(List<DelayedActionConfig> delayedActionConfigs, AServerChannelUserId user, Long initialMessage, FeatureConfig featureConfig) {
SetupSummaryStepParameter parameter = SetupSummaryStepParameter

View File

@@ -0,0 +1 @@
<#include "setup_no_channel_provided_exception_text">

View File

@@ -0,0 +1 @@
<#assign text><#if templateKey?has_content><#assign exceptionModel=templateModel><#include "${templateKey}"><#else>${message}</#if></#assign><#include "setup_post_target_exception_text">

View File

@@ -0,0 +1 @@
<#include "setup_system_config_exception_text">

View File

@@ -0,0 +1 @@
Failed to configure system config: ${message}.