migrated some exceptions to use templates

fixed missed hard coded value in template
fixed listing of channels in channel groups
fixed template loading
This commit is contained in:
Sheldan
2020-05-13 17:04:21 +02:00
parent c429aa882b
commit 80505d1014
43 changed files with 332 additions and 108 deletions

View File

@@ -1,6 +1,6 @@
package dev.sheldan.abstracto.core.service;
import dev.sheldan.abstracto.core.command.exception.ChannelGroupException;
import dev.sheldan.abstracto.core.command.exception.ChannelGroupNotFoundException;
import dev.sheldan.abstracto.core.command.exception.CommandException;
import dev.sheldan.abstracto.core.command.models.database.ACommand;
import dev.sheldan.abstracto.core.command.service.management.ChannelGroupCommandManagementService;
@@ -68,7 +68,7 @@ public class ChannelGroupServiceBean implements ChannelGroupService {
AServer server = serverManagementService.loadOrCreate(channel.getServer().getId());
AChannelGroup channelGroup = channelGroupManagementService.findByNameAndServer(channelGroupName, server);
if(channelGroup == null) {
throw new ChannelGroupException(String.format(CHANNEL_GROUP_NOT_FOUND, channelGroupName));
throw new ChannelGroupNotFoundException(channelGroupName, channelGroupManagementService.getAllAvailableAsString(server));
}
channelGroupManagementService.addChannelToChannelGroup(channelGroup, channel);
}
@@ -90,7 +90,7 @@ public class ChannelGroupServiceBean implements ChannelGroupService {
AServer server = serverManagementService.loadOrCreate(channel.getServer().getId());
AChannelGroup channelGroup = channelGroupManagementService.findByNameAndServer(channelGroupName, server);
if(channelGroup == null) {
throw new ChannelGroupException(String.format(CHANNEL_GROUP_NOT_FOUND, channelGroupName));
throw new ChannelGroupNotFoundException(channelGroupName, channelGroupManagementService.getAllAvailableAsString(server));
}
channelGroupManagementService.removeChannelFromChannelGroup(channelGroup, channel);
}
@@ -100,7 +100,7 @@ public class ChannelGroupServiceBean implements ChannelGroupService {
AServer server = serverManagementService.loadOrCreate(serverId);
AChannelGroup channelGroup = channelGroupManagementService.findByNameAndServer(channelGroupName, server);
if(channelGroup == null) {
throw new ChannelGroupException(String.format(CHANNEL_GROUP_NOT_FOUND, channelGroupName));
throw new ChannelGroupNotFoundException(channelGroupName, channelGroupManagementService.getAllAvailableAsString(server));
}
ACommand command = commandManagementService.findCommandByName(commandName);
if(command == null) {
@@ -114,7 +114,7 @@ public class ChannelGroupServiceBean implements ChannelGroupService {
AServer server = serverManagementService.loadOrCreate(serverId);
AChannelGroup channelGroup = channelGroupManagementService.findByNameAndServer(channelGroupName, server);
if(channelGroup == null) {
throw new ChannelGroupException(String.format(CHANNEL_GROUP_NOT_FOUND, channelGroupName));
throw new ChannelGroupNotFoundException(channelGroupName, channelGroupManagementService.getAllAvailableAsString(server));
}
ACommand command = commandManagementService.findCommandByName(commandName);
if(command == null) {

View File

@@ -1,6 +1,6 @@
package dev.sheldan.abstracto.core.service;
import dev.sheldan.abstracto.core.exception.ConfigurationException;
import dev.sheldan.abstracto.core.exception.ConfigurationKeyNotFoundException;
import dev.sheldan.abstracto.core.service.management.ConfigManagementService;
import dev.sheldan.abstracto.core.models.database.AConfig;
import org.springframework.beans.factory.annotation.Autowired;
@@ -54,7 +54,7 @@ public class ConfigServiceBean implements ConfigService{
if(configManagementService.configExists(serverId, name)) {
configManagementService.setDoubleValue(serverId, name, value);
} else {
throw new ConfigurationException(String.format("Key %s does not exist.", name));
throw new ConfigurationKeyNotFoundException(name);
}
}
@@ -63,7 +63,7 @@ public class ConfigServiceBean implements ConfigService{
if(configManagementService.configExists(serverId, name)) {
configManagementService.setLongValue(serverId, name, value);
} else {
throw new ConfigurationException(String.format("Key %s does not exist.", name));
throw new ConfigurationKeyNotFoundException(name);
}
}
@@ -79,7 +79,7 @@ public class ConfigServiceBean implements ConfigService{
setStringValue(name, serverId, value);
}
} else {
throw new ConfigurationException(String.format("Key %s does not exist.", name));
throw new ConfigurationKeyNotFoundException(name);
}
}
@@ -89,7 +89,7 @@ public class ConfigServiceBean implements ConfigService{
if(configManagementService.configExists(serverId, name)) {
configManagementService.setStringValue(serverId, name, value);
} else {
throw new ConfigurationException(String.format("Key %s does not exist.", name));
throw new ConfigurationKeyNotFoundException(name);
}
}
}

View File

@@ -1,7 +1,7 @@
package dev.sheldan.abstracto.core.service;
import dev.sheldan.abstracto.core.config.DynamicKeyLoader;
import dev.sheldan.abstracto.core.exception.EmoteException;
import dev.sheldan.abstracto.core.exception.EmoteNotDefinedException;
import dev.sheldan.abstracto.core.models.database.AEmote;
import dev.sheldan.abstracto.core.service.management.EmoteManagementService;
import lombok.extern.slf4j.Slf4j;
@@ -79,7 +79,7 @@ public class EmoteServiceBean implements EmoteService {
@Override
public void throwIfEmoteDoesNotExist(String emoteKey, Long serverId) {
if(!emoteManagementService.loadEmoteByName(emoteKey, serverId).isPresent()) {
throw new EmoteException(String.format("Emote %s not defined.", emoteKey));
throw new EmoteNotDefinedException(emoteKey);
}
}

View File

@@ -1,8 +1,7 @@
package dev.sheldan.abstracto.core.service;
import dev.sheldan.abstracto.core.exception.EmoteException;
import dev.sheldan.abstracto.core.exception.EmoteNotDefinedException;
import dev.sheldan.abstracto.core.exception.GuildException;
import dev.sheldan.abstracto.core.models.ServerChannelMessage;
import dev.sheldan.abstracto.core.models.database.AChannel;
import dev.sheldan.abstracto.core.models.database.AUserInAServer;
import dev.sheldan.abstracto.core.service.management.EmoteManagementService;
@@ -14,8 +13,6 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
@@ -52,7 +49,7 @@ public class MessageServiceBean implements MessageService {
message.addReaction(emoteById).queue();
} else {
log.error("Emote with key {} and id {} for guild {} was not found.", emoteKey, emote.getEmoteId(), guild.getId());
throw new EmoteException(String.format("Emote with key `%s` and id %s in guild %s was not found. Check whether or not the configured emote is available.", emoteKey, emote.getEmoteId(), guild.getIdLong()));
throw new EmoteNotDefinedException(emoteKey);
}
} else {
message.addReaction(emote.getEmoteKey()).queue();

View File

@@ -3,7 +3,7 @@ package dev.sheldan.abstracto.core.service;
import dev.sheldan.abstracto.core.config.DynamicKeyLoader;
import dev.sheldan.abstracto.core.exception.ChannelNotFoundException;
import dev.sheldan.abstracto.core.exception.GuildException;
import dev.sheldan.abstracto.core.exception.PostTargetException;
import dev.sheldan.abstracto.core.exception.PostTargetNotValidException;
import dev.sheldan.abstracto.core.service.management.PostTargetManagement;
import dev.sheldan.abstracto.core.models.database.PostTarget;
import dev.sheldan.abstracto.templating.model.MessageToSend;
@@ -70,7 +70,7 @@ public class PostTargetServiceBean implements PostTargetService {
return postTarget;
} else {
log.error("PostTarget {} in server {} was not found!", postTargetName, serverId);
throw new PostTargetException(String.format("Incorrect post target configuration: Post target %s was not found.", postTargetName));
throw new PostTargetNotValidException(postTargetName);
}
}
@@ -171,7 +171,7 @@ public class PostTargetServiceBean implements PostTargetService {
public void throwIfPostTargetIsNotDefined(String name, Long serverId) {
PostTarget postTarget = postTargetManagement.getPostTarget(name, serverId);
if(postTarget == null) {
throw new PostTargetException(String.format("Post target %s is not defined.", name));
throw new PostTargetNotValidException(name);
}
}

View File

@@ -1,7 +1,8 @@
package dev.sheldan.abstracto.core.service;
import dev.sheldan.abstracto.core.exception.GuildException;
import dev.sheldan.abstracto.core.exception.RoleException;
import dev.sheldan.abstracto.core.exception.RoleNotFoundInDBException;
import dev.sheldan.abstracto.core.exception.RoleNotFoundInGuildException;
import dev.sheldan.abstracto.core.models.database.ARole;
import dev.sheldan.abstracto.core.models.database.AServer;
import dev.sheldan.abstracto.core.models.database.AUserInAServer;
@@ -10,7 +11,6 @@ import lombok.extern.slf4j.Slf4j;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.Role;
import org.aspectj.lang.annotation.Around;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@@ -37,7 +37,7 @@ public class RoleServiceBean implements RoleService {
if(roleById != null) {
guild.addRoleToMember(aUserInAServer.getUserReference().getId(), roleById).queue();
} else {
throw new RoleException(String.format("Failed to load role %s in guild %s", role.getId(), aUserInAServer.getServerReference().getId()));
throw new RoleNotFoundInGuildException(role.getId(), aUserInAServer.getServerReference().getId());
}
} else {
throw new GuildException(String.format("Failed to load guild %s.", aUserInAServer.getServerReference().getId()));
@@ -53,7 +53,7 @@ public class RoleServiceBean implements RoleService {
if(roleById != null) {
guild.removeRoleFromMember(aUserInAServer.getUserReference().getId(), roleById).queue();
} else {
throw new RoleException(String.format("Failed to load role %s in guild %s", role.getId(), aUserInAServer.getServerReference().getId()));
throw new RoleNotFoundInGuildException(role.getId(), aUserInAServer.getServerReference().getId());
}
} else {
throw new GuildException(String.format("Failed to load guild %s.", aUserInAServer.getServerReference().getId()));
@@ -68,7 +68,7 @@ public class RoleServiceBean implements RoleService {
@Override
public void markDeleted(Long id, AServer server) {
Optional<ARole> role = roleManagementService.findRole(id, server);
ARole role1 = role.orElseThrow(() -> new RoleException(String.format("Cannot find role %s to mark as deleted.", id)));
ARole role1 = role.orElseThrow(() -> new RoleNotFoundInDBException(id, server.getId()));
roleManagementService.markDeleted(role1);
}

View File

@@ -1,6 +1,5 @@
package dev.sheldan.abstracto.core.service;
import dev.sheldan.abstracto.core.exception.UndoActionException;
import dev.sheldan.abstracto.core.models.UndoAction;
import dev.sheldan.abstracto.core.models.UndoActionInstance;
import lombok.extern.slf4j.Slf4j;

View File

@@ -1,7 +1,7 @@
package dev.sheldan.abstracto.core.service.management;
import dev.sheldan.abstracto.core.command.exception.ChannelGroupException;
import dev.sheldan.abstracto.core.exception.ChannelNotFoundException;
import dev.sheldan.abstracto.core.command.exception.ChannelGroupExistsException;
import dev.sheldan.abstracto.core.command.exception.ChannelGroupNotFoundException;
import dev.sheldan.abstracto.core.models.database.AChannel;
import dev.sheldan.abstracto.core.models.database.AChannelGroup;
import dev.sheldan.abstracto.core.models.database.AServer;
@@ -11,6 +11,7 @@ import org.springframework.stereotype.Component;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
@Component
public class ChannelGroupManagementServiceBean implements ChannelGroupManagementService {
@@ -25,7 +26,7 @@ public class ChannelGroupManagementServiceBean implements ChannelGroupManagement
public AChannelGroup createChannelGroup(String name, AServer server) {
name = name.toLowerCase();
if(doesChannelGroupExist(name, server)) {
throw new ChannelGroupException("Channel group already exists.");
throw new ChannelGroupExistsException(name);
}
AChannelGroup channelGroup = AChannelGroup
.builder()
@@ -46,7 +47,7 @@ public class ChannelGroupManagementServiceBean implements ChannelGroupManagement
name = name.toLowerCase();
AChannelGroup existing = findByNameAndServer(name, server);
if(existing == null) {
throw new ChannelGroupException(String.format("Channel group %s does not exist", name));
throw new ChannelGroupNotFoundException(name, getAllAvailableAsString(server));
}
channelGroupRepository.delete(existing);
}
@@ -54,11 +55,8 @@ public class ChannelGroupManagementServiceBean implements ChannelGroupManagement
@Override
public AChannelGroup addChannelToChannelGroup(AChannelGroup channelGroup, AChannel channel) {
Predicate<AChannel> channelInGroupPredicate = channel1 -> channel1.getId().equals(channel.getId());
if(channelGroup == null) {
throw new ChannelGroupException("Channel group was not found.");
}
if(channelGroup.getChannels().stream().anyMatch(channelInGroupPredicate)) {
throw new ChannelGroupException(String.format("Channel %s is already part of group %s.", channel.getId(), channelGroup.getGroupName()));
throw new ChannelGroupExistsException(String.format("Channel %s is already part of group %s.", channel.getId(), channelGroup.getGroupName()));
}
channelGroup.getChannels().add(channel);
channel.getGroups().add(channelGroup);
@@ -69,7 +67,7 @@ public class ChannelGroupManagementServiceBean implements ChannelGroupManagement
public void removeChannelFromChannelGroup(AChannelGroup channelGroup, AChannel channel) {
Predicate<AChannel> channelInGroupPredicate = channel1 -> channel1.getId().equals(channel.getId());
if(channelGroup.getChannels().stream().noneMatch(channelInGroupPredicate)) {
throw new ChannelGroupException(String.format("Channel %s is not part of group %s.", channel.getId(), channelGroup.getGroupName()));
throw new ChannelGroupExistsException(String.format("Channel %s is not part of group %s.", channel.getId(), channelGroup.getGroupName()));
}
channelGroup.getChannels().removeIf(channelInGroupPredicate);
channel.getGroups().removeIf(channelGroup1 -> channelGroup1.getId().equals(channelGroup.getId()));
@@ -86,6 +84,11 @@ public class ChannelGroupManagementServiceBean implements ChannelGroupManagement
return channelGroupRepository.findByServer(server);
}
@Override
public List<String> getAllAvailableAsString(AServer server) {
return findAllInServer(server).stream().map(AChannelGroup::getGroupName).collect(Collectors.toList());
}
@Override
public List<AChannelGroup> findAllInServer(Long serverId) {
AServer server = serverManagementService.loadOrCreate(serverId);

View File

@@ -1,7 +1,7 @@
package dev.sheldan.abstracto.core.service.management;
import dev.sheldan.abstracto.core.config.DynamicKeyLoader;
import dev.sheldan.abstracto.core.exception.EmoteException;
import dev.sheldan.abstracto.core.exception.EmoteNotFoundException;
import dev.sheldan.abstracto.core.models.database.AEmote;
import dev.sheldan.abstracto.core.models.database.AServer;
import dev.sheldan.abstracto.core.repository.EmoteRepository;
@@ -148,7 +148,7 @@ public class EmoteManagementServiceBean implements EmoteManagementService {
private void validateEmoteName(String name) {
List<String> possibleEmotes = dynamicKeyLoader.getEmoteNamesAsList();
if(!possibleEmotes.contains(name)) {
throw new EmoteException("Emote `" + name + "` is not defined. Possible values are: " + String.join(", ", possibleEmotes));
throw new EmoteNotFoundException(name, possibleEmotes);
}
}
}

View File

@@ -2,7 +2,7 @@ package dev.sheldan.abstracto.core.service.management;
import dev.sheldan.abstracto.core.config.DynamicKeyLoader;
import dev.sheldan.abstracto.core.exception.ChannelNotFoundException;
import dev.sheldan.abstracto.core.exception.PostTargetException;
import dev.sheldan.abstracto.core.exception.PostTargetNotFoundException;
import dev.sheldan.abstracto.core.models.database.AChannel;
import dev.sheldan.abstracto.core.models.database.AServer;
import dev.sheldan.abstracto.core.models.database.PostTarget;
@@ -36,7 +36,7 @@ public class PostTargetManagementBean implements PostTargetManagement {
@Override
public PostTarget createPostTarget(String name, AServer server, AChannel targetChannel) {
if(!postTargetService.validPostTarget(name)) {
throw new PostTargetException("PostTarget not found. Possible values are: " + String.join(", ", dynamicKeyLoader.getPostTargetsAsList()));
throw new PostTargetNotFoundException(name, dynamicKeyLoader.getPostTargetsAsList());
}
log.info("Creating post target {} pointing towards {}", name, targetChannel);
PostTarget build = PostTarget.builder().name(name).channelReference(targetChannel).serverReference(server).build();

View File

@@ -15,10 +15,7 @@
{
"name": "${group.name}",
"value": "
<#list group.channels as channel>
${channel.discordChannel.asMention}
<#sep>,
</#list>
<#list group.channels as channel>${channel.discordChannel.asMention}<#sep>,</#list>
"
}<#sep>,
<#else>