refactored channel exception to have a unified interface to form the exception message

replaced getOne with findById in order to get optionals and handle those
some places still have the general abstracto run time exception
This commit is contained in:
Sheldan
2020-05-13 12:49:08 +02:00
parent 5755d033eb
commit c429aa882b
52 changed files with 501 additions and 344 deletions

View File

@@ -11,6 +11,7 @@ import dev.sheldan.abstracto.core.command.service.PostCommandExecution;
import dev.sheldan.abstracto.core.command.execution.*;
import dev.sheldan.abstracto.core.command.execution.UnParsedCommandParameter;
import dev.sheldan.abstracto.core.Constants;
import dev.sheldan.abstracto.core.exception.ChannelNotFoundException;
import dev.sheldan.abstracto.core.models.database.ARole;
import dev.sheldan.abstracto.core.service.management.ChannelManagementService;
import dev.sheldan.abstracto.core.service.management.RoleManagementService;
@@ -116,12 +117,13 @@ public class CommandReceivedHandler extends ListenerAdapter {
}
private UserInitiatedServerContext buildTemplateParameter(MessageReceivedEvent event) {
AChannel channel = channelManagementService.loadChannel(event.getChannel().getIdLong());
Optional<AChannel> channel = channelManagementService.loadChannel(event.getChannel().getIdLong());
AServer server = serverManagementService.loadOrCreate(event.getGuild().getIdLong());
AUserInAServer user = userInServerManagementService.loadUser(event.getMember());
AChannel channel1 = channel.orElseThrow(() -> new ChannelNotFoundException(event.getChannel().getIdLong(), event.getGuild().getIdLong()));
return UserInitiatedServerContext
.builder()
.channel(channel)
.channel(channel1)
.server(server)
.member(event.getMember())
.aUserInAServer(user)

View File

@@ -2,9 +2,8 @@ package dev.sheldan.abstracto.core.listener;
import dev.sheldan.abstracto.core.service.management.ChannelManagementService;
import dev.sheldan.abstracto.core.models.database.AChannelType;
import dev.sheldan.abstracto.core.models.database.AChannel;
import dev.sheldan.abstracto.core.models.database.AServer;
import dev.sheldan.abstracto.core.repository.ServerRepository;
import dev.sheldan.abstracto.core.service.management.ServerManagementService;
import lombok.extern.slf4j.Slf4j;
import net.dv8tion.jda.api.entities.TextChannel;
import net.dv8tion.jda.api.events.channel.text.TextChannelCreateEvent;
@@ -21,7 +20,7 @@ import javax.annotation.Nonnull;
public class ChannelListener extends ListenerAdapter {
@Autowired
private ServerRepository serverRepository;
private ServerManagementService serverManagementService;
@Autowired
private ChannelManagementService channelManagementService;
@@ -37,7 +36,7 @@ public class ChannelListener extends ListenerAdapter {
@Transactional
public void onTextChannelCreate(@Nonnull TextChannelCreateEvent event) {
log.info("Handling channel created event. Channel {}, Server {}", event.getChannel().getIdLong(), event.getGuild().getIdLong());
AServer serverObject = serverRepository.getOne(event.getGuild().getIdLong());
AServer serverObject = serverManagementService.loadOrCreate(event.getGuild().getIdLong());
TextChannel createdChannel = event.getChannel();
AChannelType type = AChannelType.getAChannelType(createdChannel.getType());
channelManagementService.createChannel(createdChannel.getIdLong(), type, serverObject);

View File

@@ -1,6 +1,6 @@
package dev.sheldan.abstracto.core.service;
import dev.sheldan.abstracto.core.exception.ChannelException;
import dev.sheldan.abstracto.core.exception.ChannelNotFoundException;
import dev.sheldan.abstracto.core.exception.GuildException;
import dev.sheldan.abstracto.core.models.GuildChannelMember;
import dev.sheldan.abstracto.core.models.database.AEmote;
@@ -55,7 +55,7 @@ public class BotServiceBean implements BotService {
Member member = guild.getMemberById(userId);
return GuildChannelMember.builder().guild(guild).textChannel(textChannel).member(member).build();
} else {
throw new ChannelException(String.format("Text channel %s not found in guild %s", channelId, serverId));
throw new ChannelNotFoundException(channelId, serverId);
}
}
else {

View File

@@ -5,6 +5,7 @@ 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;
import dev.sheldan.abstracto.core.command.service.management.CommandManagementService;
import dev.sheldan.abstracto.core.exception.ChannelNotFoundException;
import dev.sheldan.abstracto.core.models.database.AChannel;
import dev.sheldan.abstracto.core.models.database.AChannelGroup;
import dev.sheldan.abstracto.core.models.database.AServer;
@@ -15,6 +16,8 @@ import net.dv8tion.jda.api.entities.TextChannel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Optional;
@Component
public class ChannelGroupServiceBean implements ChannelGroupService {
@@ -50,13 +53,14 @@ public class ChannelGroupServiceBean implements ChannelGroupService {
@Override
public void addChannelToChannelGroup(String channelGroupName, TextChannel textChannel) {
addChannelToChannelGroup(channelGroupName, textChannel.getIdLong());
addChannelToChannelGroup(channelGroupName, textChannel.getIdLong(), textChannel.getGuild().getIdLong());
}
@Override
public void addChannelToChannelGroup(String channelGroupName, Long channelId) {
AChannel aChannel = channelManagementService.loadChannel(channelId);
addChannelToChannelGroup(channelGroupName, aChannel);
public void addChannelToChannelGroup(String channelGroupName, Long channelId, Long serverId) {
Optional<AChannel> aChannel = channelManagementService.loadChannel(channelId);
AChannel channel = aChannel.orElseThrow(() -> new ChannelNotFoundException(channelId, serverId));
addChannelToChannelGroup(channelGroupName, channel);
}
@Override
@@ -71,13 +75,14 @@ public class ChannelGroupServiceBean implements ChannelGroupService {
@Override
public void removeChannelFromChannelGroup(String channelGroupName, TextChannel textChannel) {
removeChannelFromChannelGroup(channelGroupName, textChannel.getIdLong());
removeChannelFromChannelGroup(channelGroupName, textChannel.getIdLong(), textChannel.getGuild().getIdLong());
}
@Override
public void removeChannelFromChannelGroup(String channelGroupName, Long channelId) {
AChannel aChannel = channelManagementService.loadChannel(channelId);
removeChannelFromChannelGroup(channelGroupName, aChannel);
public void removeChannelFromChannelGroup(String channelGroupName, Long channelId, Long serverId) {
Optional<AChannel> aChannel = channelManagementService.loadChannel(channelId);
AChannel channel = aChannel.orElseThrow(() -> new ChannelNotFoundException(channelId, serverId));
removeChannelFromChannelGroup(channelGroupName, channel);
}
@Override

View File

@@ -1,7 +1,7 @@
package dev.sheldan.abstracto.core.service;
import dev.sheldan.abstracto.core.exception.AbstractoRunTimeException;
import dev.sheldan.abstracto.core.exception.ChannelException;
import dev.sheldan.abstracto.core.exception.ChannelNotFoundException;
import dev.sheldan.abstracto.core.exception.GuildException;
import dev.sheldan.abstracto.core.models.database.AServer;
import dev.sheldan.abstracto.templating.model.MessageToSend;
@@ -15,7 +15,6 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import java.nio.channels.Channel;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
@@ -50,7 +49,7 @@ public class ChannelServiceBean implements ChannelService {
return sendTextToChannel(text, textChannel);
} else {
log.error("Channel {} to post towards was not found in server {}", channel.getId(), channel.getServer().getId());
throw new ChannelException(String.format("Channel %s to post to not found.", channel.getId()));
throw new ChannelNotFoundException(channel.getId(), channel.getServer().getId());
}
} else {
log.error("Guild {} was not found when trying to post a message", channel.getServer().getId());
@@ -65,7 +64,7 @@ public class ChannelServiceBean implements ChannelService {
TextChannel textChannel = textChannelOpt.get();
return sendMessageToChannel(message, textChannel);
}
throw new ChannelException(String.format("Channel %s in guild %s not found.", channel.getId(), channel.getServer().getId()));
throw new ChannelNotFoundException(channel.getId(), channel.getServer().getId());
}
@Override
@@ -87,7 +86,7 @@ public class ChannelServiceBean implements ChannelService {
return sendEmbedToChannel(embed, textChannel);
} else {
log.error("Channel {} to post towards was not found in server {}", channel.getId(), channel.getServer().getId());
throw new ChannelException(String.format("Channel %s to post to not found.", channel.getId()));
throw new ChannelNotFoundException(channel.getId(), guild.getIdLong());
}
} else {
log.error("Guild {} was not found when trying to post a message", channel.getServer().getId());
@@ -106,7 +105,7 @@ public class ChannelServiceBean implements ChannelService {
if(textChannelFromServer.isPresent()) {
return sendMessageToSendToChannel(messageToSend, textChannelFromServer.get());
}
throw new ChannelException(String.format("Channel %s was not found.", channel.getId()));
throw new ChannelNotFoundException(channel.getId(), channel.getServer().getId());
}
@Override
@@ -144,7 +143,7 @@ public class ChannelServiceBean implements ChannelService {
TextChannel textChannel = textChannelFromServer.get();
editMessageInAChannel(messageToSend, textChannel, messageId);
} else {
throw new ChannelException(String.format("Channel %s was not found.", channel.getId()));
throw new ChannelNotFoundException(channel.getId(), channel.getServer().getId());
}
}
@@ -177,7 +176,7 @@ public class ChannelServiceBean implements ChannelService {
if(textChannelById != null) {
return textChannelById.delete().submit();
}
throw new ChannelException(String.format("Failed to delete channel %s in server %s", channelId, serverId));
throw new ChannelNotFoundException(channelId, serverId);
}
@Override

View File

@@ -1,6 +1,6 @@
package dev.sheldan.abstracto.core.service;
import dev.sheldan.abstracto.core.exception.ChannelException;
import dev.sheldan.abstracto.core.exception.ChannelNotFoundException;
import dev.sheldan.abstracto.core.exception.GuildException;
import dev.sheldan.abstracto.core.models.cache.CachedMessage;
import dev.sheldan.abstracto.core.models.cache.CachedReaction;
@@ -100,7 +100,7 @@ public class MessageCacheBean implements MessageCache {
);
} else {
log.error("Not able to load message {} in channel {} in guild {}. Text channel not found.", messageId, textChannelId, guildId);
future.completeExceptionally(new ChannelException(String.format("Not able to load message %s. Text channel %s not found in guild %s", messageId, textChannelId, guildId)));
future.completeExceptionally(new ChannelNotFoundException(textChannelId, guildId));
}
} else {
log.error("Not able to load message {} in channel {} in guild {}. Guild not found.", messageId, textChannelId, guildId);

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.ChannelException;
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.service.management.PostTargetManagement;
@@ -57,8 +57,7 @@ public class PostTargetServiceBean implements PostTargetService {
} else {
log.error("Incorrect post target configuration: {} points to {} on server {}", target.getName(),
target.getChannelReference().getId(), target.getServerReference().getId());
throw new ChannelException(String.format("Incorrect post target configuration. The channel %s of target %s cannot be found",
target.getChannelReference().getId(), target.getChannelReference().getId()));
throw new ChannelNotFoundException(target.getChannelReference().getId(), target.getServerReference().getId());
}
} else {
throw new GuildException(String.format("Incorrect post target configuration. Guild %s cannot be found.", target.getServerReference().getId()));
@@ -172,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 ChannelException(String.format("Post target %s is not defined.", name));
throw new PostTargetException(String.format("Post target %s is not defined.", name));
}
}

View File

@@ -10,6 +10,7 @@ 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;
@@ -66,12 +67,9 @@ public class RoleServiceBean implements RoleService {
@Override
public void markDeleted(Long id, AServer server) {
ARole role = roleManagementService.findRole(id, server);
if(role != null) {
roleManagementService.markDeleted(role);
} else {
throw new RoleException(String.format("Cannot find role %s to mark as deleted.", id));
}
Optional<ARole> role = roleManagementService.findRole(id, server);
ARole role1 = role.orElseThrow(() -> new RoleException(String.format("Cannot find role %s to mark as deleted.", id)));
roleManagementService.markDeleted(role1);
}
@Override

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.ChannelException;
import dev.sheldan.abstracto.core.exception.ChannelNotFoundException;
import dev.sheldan.abstracto.core.models.database.AChannel;
import dev.sheldan.abstracto.core.models.database.AChannelGroup;
import dev.sheldan.abstracto.core.models.database.AServer;
@@ -25,7 +25,7 @@ public class ChannelGroupManagementServiceBean implements ChannelGroupManagement
public AChannelGroup createChannelGroup(String name, AServer server) {
name = name.toLowerCase();
if(doesChannelGroupExist(name, server)) {
throw new ChannelException("Channel group already exists.");
throw new ChannelGroupException("Channel group already exists.");
}
AChannelGroup channelGroup = AChannelGroup
.builder()
@@ -58,7 +58,7 @@ public class ChannelGroupManagementServiceBean implements ChannelGroupManagement
throw new ChannelGroupException("Channel group was not found.");
}
if(channelGroup.getChannels().stream().anyMatch(channelInGroupPredicate)) {
throw new ChannelException(String.format("Channel %s is already part of group %s.", channel.getId(), channelGroup.getGroupName()));
throw new ChannelGroupException(String.format("Channel %s is already part of group %s.", channel.getId(), channelGroup.getGroupName()));
}
channelGroup.getChannels().add(channel);
channel.getGroups().add(channelGroup);
@@ -69,7 +69,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 ChannelException(String.format("Channel %s is not part of group %s.", channel.getId(), channelGroup.getGroupName()));
throw new ChannelGroupException(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()));

View File

@@ -1,6 +1,7 @@
package dev.sheldan.abstracto.core.service.management;
import dev.sheldan.abstracto.core.command.models.TableLocks;
import dev.sheldan.abstracto.core.exception.ChannelNotFoundException;
import dev.sheldan.abstracto.core.models.database.AChannel;
import dev.sheldan.abstracto.core.models.database.AChannelType;
import dev.sheldan.abstracto.core.models.database.AServer;
@@ -10,6 +11,8 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Optional;
@Service
@Slf4j
public class ChannelManagementServiceBean implements ChannelManagementService {
@@ -21,8 +24,8 @@ public class ChannelManagementServiceBean implements ChannelManagementService {
private LockService lockService;
@Override
public AChannel loadChannel(Long id) {
return repository.getOne(id);
public Optional<AChannel> loadChannel(Long id) {
return repository.findById(id);
}
@Override
@@ -39,13 +42,14 @@ public class ChannelManagementServiceBean implements ChannelManagementService {
.build();
return repository.save(build);
} else {
return loadChannel(id);
return loadChannel(id).get();
}
}
@Override
public AChannel markAsDeleted(Long id) {
AChannel channel = loadChannel(id);
Optional<AChannel> channelOptional = loadChannel(id);
AChannel channel = channelOptional.orElseThrow(() -> new ChannelNotFoundException(id, 0L));
channel.setDeleted(true);
return channel;
}

View File

@@ -25,8 +25,8 @@ public class EmoteManagementServiceBean implements EmoteManagementService {
private DynamicKeyLoader dynamicKeyLoader;
@Override
public AEmote loadEmote(Long id) {
return repository.getOne(id);
public Optional<AEmote> loadEmote(Long id) {
return repository.findById(id);
}
@Override

View File

@@ -1,6 +1,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.models.database.AChannel;
import dev.sheldan.abstracto.core.models.database.AServer;
@@ -11,6 +12,8 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Optional;
@Service
@Slf4j
public class PostTargetManagementBean implements PostTargetManagement {
@@ -53,13 +56,15 @@ public class PostTargetManagementBean implements PostTargetManagement {
@Override
public PostTarget createOrUpdate(String name, AServer server, Long channelId) {
AChannel dbChannel = channelManagementService.loadChannel(channelId);
Optional<AChannel> dbChannelOpt = channelManagementService.loadChannel(channelId);
AChannel dbChannel = dbChannelOpt.orElseThrow(() -> new ChannelNotFoundException(channelId, server.getId()));
return createOrUpdate(name, server, dbChannel);
}
@Override
public PostTarget createOrUpdate(String name, Long serverId, Long channelId) {
AChannel dbChannel = channelManagementService.loadChannel(channelId);
Optional<AChannel> dbChannelOpt = channelManagementService.loadChannel(channelId);
AChannel dbChannel = dbChannelOpt.orElseThrow(() -> new ChannelNotFoundException(channelId, serverId));
AServer dbServer = serverManagementService.loadOrCreate(serverId);
return createOrUpdate(name, dbServer, dbChannel);
}

View File

@@ -6,6 +6,8 @@ import dev.sheldan.abstracto.core.repository.RoleRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Optional;
@Service
public class RoleManagementServiceBean implements RoleManagementService {
@@ -24,8 +26,8 @@ public class RoleManagementServiceBean implements RoleManagementService {
}
@Override
public ARole findRole(Long id, AServer server) {
return repository.getOne(id);
public Optional<ARole> findRole(Long id, AServer server) {
return repository.findById(id);
}
@Override

View File

@@ -1,5 +1,6 @@
package dev.sheldan.abstracto.core.service.management;
import dev.sheldan.abstracto.core.exception.AbstractoRunTimeException;
import dev.sheldan.abstracto.core.models.database.*;
import dev.sheldan.abstracto.core.repository.ServerRepository;
import lombok.extern.slf4j.Slf4j;
@@ -7,6 +8,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
@Service
@Slf4j
@@ -29,7 +31,7 @@ public class ServerManagementServiceBean implements ServerManagementService {
@Override
public AServer loadOrCreate(Long id) {
if(repository.existsById(id)) {
return repository.getOne(id);
return repository.findById(id).get();
} else {
return createServer(id);
}
@@ -49,10 +51,11 @@ public class ServerManagementServiceBean implements ServerManagementService {
@Override
public AUserInAServer addUserToServer(Long serverId, Long userId) {
log.info("Adding user {} to server {}", userId, serverId);
AServer server = repository.getOne(serverId);
Optional<AServer> server = repository.findById(serverId);
AUser user = userManagementService.loadUser(userId);
AUserInAServer aUserInAServer = AUserInAServer.builder().serverReference(server).userReference(user).build();
server.getUsers().add(aUserInAServer);
AServer serverReference = server.orElseThrow(() -> new AbstractoRunTimeException(String.format("Cannnot find server %s", serverId)));
AUserInAServer aUserInAServer = AUserInAServer.builder().serverReference(serverReference).userReference(user).build();
serverReference.getUsers().add(aUserInAServer);
return aUserInAServer;
}

View File

@@ -10,6 +10,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Optional;
@Component
@Slf4j
@@ -47,8 +48,8 @@ public class UserInServerManagementServiceBean implements UserInServerManagement
}
@Override
public AUserInAServer loadUser(Long userInServerId) {
return userInServerRepository.getOne(userInServerId);
public Optional<AUserInAServer> loadUser(Long userInServerId) {
return userInServerRepository.findById(userInServerId);
}
@Override

View File

@@ -30,7 +30,7 @@ public class UserManagementServiceBean implements UserManagementService {
@Override
public AUser loadUser(Long userId) {
if(userRepository.existsById(userId)) {
return userRepository.getOne(userId);
return userRepository.findById(userId).get();
} else {
return this.createUser(userId);
}