mirror of
https://github.com/Sheldan/abstracto.git
synced 2026-03-19 20:21:14 +00:00
moved some more exceptions to templates
made more specific exceptions for certain cases
This commit is contained in:
@@ -50,7 +50,7 @@ public class BanServiceBean implements BanService {
|
||||
guildByIdOptional.get().ban(userId.toString(), 0, reason).queue();
|
||||
} else {
|
||||
log.warn("Guild {} not found. Not able to ban user {}", guildId, userId);
|
||||
throw new GuildException(String.format("Guild %s not found. Not able to ban user %s", guildId, userId));
|
||||
throw new GuildException(guildId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,7 +37,7 @@ public class KickServiceBean implements KickService {
|
||||
this.sendKickLog(kickLogModel);
|
||||
} else {
|
||||
log.warn("Not able to kick. Guild {} not found.", kickLogModel.getGuild().getIdLong());
|
||||
throw new GuildException(String.format("Not able to kick %s. Guild %s not found", kickLogModel.getMember().getIdLong(), kickLogModel.getGuild().getIdLong()));
|
||||
throw new GuildException(kickLogModel.getGuild().getIdLong());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -59,7 +59,7 @@ public class BotServiceBean implements BotService {
|
||||
}
|
||||
}
|
||||
else {
|
||||
throw new GuildException(String.format(GUILD_NOT_FOUND, serverId));
|
||||
throw new GuildException(serverId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -69,7 +69,7 @@ public class BotServiceBean implements BotService {
|
||||
if(guildById != null) {
|
||||
return guildById.getMemberById(memberId);
|
||||
} else {
|
||||
throw new GuildException(String.format(GUILD_NOT_FOUND, serverId));
|
||||
throw new GuildException(serverId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -79,7 +79,7 @@ public class BotServiceBean implements BotService {
|
||||
if(guildById != null) {
|
||||
return isUserInGuild(guildById, aUserInAServer);
|
||||
} else {
|
||||
throw new GuildException(String.format(GUILD_NOT_FOUND, aUserInAServer.getServerReference().getId()));
|
||||
throw new GuildException(aUserInAServer.getServerReference().getId());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -132,7 +132,7 @@ public class BotServiceBean implements BotService {
|
||||
Emote emoteById = guild.getEmoteById(emote.getEmoteId());
|
||||
return Optional.ofNullable(emoteById);
|
||||
}
|
||||
throw new GuildException(String.format(GUILD_NOT_FOUND, serverId));
|
||||
throw new GuildException(serverId);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -147,7 +147,7 @@ public class BotServiceBean implements BotService {
|
||||
Guild guild = guildOptional.get();
|
||||
return Optional.ofNullable(guild.getTextChannelById(textChannelId));
|
||||
}
|
||||
throw new GuildException(String.format(GUILD_NOT_FOUND, serverId));
|
||||
throw new GuildException(GUILD_NOT_FOUND, serverId);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package dev.sheldan.abstracto.core.service;
|
||||
|
||||
import dev.sheldan.abstracto.core.exception.AbstractoRunTimeException;
|
||||
import dev.sheldan.abstracto.core.exception.CategoryNotFoundException;
|
||||
import dev.sheldan.abstracto.core.exception.ChannelNotFoundException;
|
||||
import dev.sheldan.abstracto.core.exception.GuildException;
|
||||
import dev.sheldan.abstracto.core.models.database.AServer;
|
||||
@@ -53,7 +54,7 @@ public class ChannelServiceBean implements ChannelService {
|
||||
}
|
||||
} else {
|
||||
log.error("Guild {} was not found when trying to post a message", channel.getServer().getId());
|
||||
throw new GuildException(String.format("Guild %s to post in channel %s was not found.", channel.getServer().getId(), channel.getId()));
|
||||
throw new GuildException(channel.getServer().getId());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -90,7 +91,7 @@ public class ChannelServiceBean implements ChannelService {
|
||||
}
|
||||
} else {
|
||||
log.error("Guild {} was not found when trying to post a message", channel.getServer().getId());
|
||||
throw new GuildException(String.format("Guild %s to post in channel %s was not found.", channel.getServer().getId(), channel.getId()));
|
||||
throw new GuildException(channel.getServer().getId());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -195,12 +196,8 @@ public class ChannelServiceBean implements ChannelService {
|
||||
if(categoryById != null) {
|
||||
return categoryById.createTextChannel(name).submit();
|
||||
}
|
||||
CompletableFuture<TextChannel> objectCompletableFuture = new CompletableFuture<>();
|
||||
objectCompletableFuture.completeExceptionally(new AbstractoRunTimeException("Mod mail category is not setup."));
|
||||
return objectCompletableFuture;
|
||||
throw new CategoryNotFoundException(categoryId, server.getId());
|
||||
}
|
||||
CompletableFuture<TextChannel> objectCompletableFuture = new CompletableFuture<>();
|
||||
objectCompletableFuture.completeExceptionally(new AbstractoRunTimeException("Guild to create mod mail channel for was not found."));
|
||||
return objectCompletableFuture;
|
||||
throw new GuildException(server.getId());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -104,8 +104,7 @@ public class MessageCacheBean implements MessageCache {
|
||||
}
|
||||
} else {
|
||||
log.error("Not able to load message {} in channel {} in guild {}. Guild not found.", messageId, textChannelId, guildId);
|
||||
future.completeExceptionally(new GuildException(String.format("Not able to load message %s. Guild %s not found.", messageId, guildId)));
|
||||
|
||||
future.completeExceptionally(new GuildException(guildId));
|
||||
}
|
||||
|
||||
return future;
|
||||
|
||||
@@ -59,7 +59,7 @@ public class MessageServiceBean implements MessageService {
|
||||
message.addReaction(defaultEmote).queue();}
|
||||
} else {
|
||||
log.error("Cannot add reaction, guild not found {}", serverId);
|
||||
throw new GuildException(String.format("Cannot add reaction, guild %s not found.", serverId));
|
||||
throw new GuildException(serverId);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -61,7 +61,7 @@ public class PostTargetServiceBean implements PostTargetService {
|
||||
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()));
|
||||
throw new GuildException(target.getServerReference().getId());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -40,7 +40,7 @@ public class RoleServiceBean implements RoleService {
|
||||
throw new RoleNotFoundInGuildException(role.getId(), aUserInAServer.getServerReference().getId());
|
||||
}
|
||||
} else {
|
||||
throw new GuildException(String.format("Failed to load guild %s.", aUserInAServer.getServerReference().getId()));
|
||||
throw new GuildException(aUserInAServer.getServerReference().getId());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -56,7 +56,7 @@ public class RoleServiceBean implements RoleService {
|
||||
throw new RoleNotFoundInGuildException(role.getId(), aUserInAServer.getServerReference().getId());
|
||||
}
|
||||
} else {
|
||||
throw new GuildException(String.format("Failed to load guild %s.", aUserInAServer.getServerReference().getId()));
|
||||
throw new GuildException(aUserInAServer.getServerReference().getId());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -78,7 +78,7 @@ public class RoleServiceBean implements RoleService {
|
||||
if(guildById.isPresent()) {
|
||||
return guildById.get().getRoleById(role.getId());
|
||||
} else {
|
||||
throw new GuildException(String.format("Failed to load guild %s.", role.getServer().getId()));
|
||||
throw new GuildException(role.getServer().getId());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package dev.sheldan.abstracto.core.service.management;
|
||||
|
||||
import dev.sheldan.abstracto.core.exception.AbstractoRunTimeException;
|
||||
import dev.sheldan.abstracto.core.exception.GuildException;
|
||||
import dev.sheldan.abstracto.core.models.database.*;
|
||||
import dev.sheldan.abstracto.core.repository.ServerRepository;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@@ -53,7 +53,7 @@ public class ServerManagementServiceBean implements ServerManagementService {
|
||||
log.info("Adding user {} to server {}", userId, serverId);
|
||||
Optional<AServer> server = repository.findById(serverId);
|
||||
AUser user = userManagementService.loadUser(userId);
|
||||
AServer serverReference = server.orElseThrow(() -> new AbstractoRunTimeException(String.format("Cannnot find server %s", serverId)));
|
||||
AServer serverReference = server.orElseThrow(() -> new GuildException(serverId));
|
||||
AUserInAServer aUserInAServer = AUserInAServer.builder().serverReference(serverReference).userReference(user).build();
|
||||
serverReference.getUsers().add(aUserInAServer);
|
||||
return aUserInAServer;
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
<#include "duration_invalid_time_format_exception_text">
|
||||
@@ -0,0 +1,34 @@
|
||||
package dev.sheldan.abstracto.core.exception;
|
||||
|
||||
import dev.sheldan.abstracto.templating.Templatable;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
public class CategoryNotFoundException extends AbstractoRunTimeException implements Templatable {
|
||||
|
||||
private Long categoryId;
|
||||
private Long guildId;
|
||||
|
||||
public CategoryNotFoundException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public CategoryNotFoundException(Long categoryId, Long guildId) {
|
||||
super("");
|
||||
this.categoryId = categoryId;
|
||||
this.guildId = guildId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getTemplateName() {
|
||||
return "category_not_found_exception";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getTemplateModel() {
|
||||
HashMap<String, Long> param = new HashMap<>();
|
||||
param.put("categoryId", this.categoryId);
|
||||
param.put("guildId", this.guildId);
|
||||
return param;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package dev.sheldan.abstracto.core.exception;
|
||||
|
||||
import dev.sheldan.abstracto.templating.Templatable;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
public class DurationFormatException extends AbstractoRunTimeException implements Templatable {
|
||||
|
||||
private String invalidFormat;
|
||||
private List<String> validFormats;
|
||||
|
||||
public DurationFormatException(String wrongFormat, List<String> validFormats) {
|
||||
super("");
|
||||
this.invalidFormat = wrongFormat;
|
||||
this.validFormats = validFormats;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getTemplateName() {
|
||||
return "duration_invalid_time_format_exception";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getTemplateModel() {
|
||||
HashMap<String, String> param = new HashMap<>();
|
||||
param.put("format", this.invalidFormat);
|
||||
param.put("valid", String.join(", ", this.validFormats));
|
||||
return param;
|
||||
}
|
||||
}
|
||||
@@ -2,18 +2,30 @@ package dev.sheldan.abstracto.core.exception;
|
||||
|
||||
import dev.sheldan.abstracto.templating.Templatable;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
public class GuildException extends AbstractoRunTimeException implements Templatable {
|
||||
public GuildException(String message) {
|
||||
private Long guildId;
|
||||
|
||||
public GuildException(String message, Long guildId) {
|
||||
super(message);
|
||||
this.guildId = guildId;
|
||||
}
|
||||
|
||||
public GuildException(Long guildId) {
|
||||
super("");
|
||||
this.guildId = guildId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getTemplateName() {
|
||||
return null;
|
||||
return "guild_not_found_exception";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getTemplateModel() {
|
||||
return null;
|
||||
HashMap<String, Long> param = new HashMap<>();
|
||||
param.put("guildId", this.guildId);
|
||||
return param;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
package dev.sheldan.abstracto.core.utils;
|
||||
|
||||
|
||||
import dev.sheldan.abstracto.core.exception.AbstractoRunTimeException;
|
||||
import dev.sheldan.abstracto.core.exception.DurationFormatException;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@@ -14,6 +16,7 @@ public class ParseUtils {
|
||||
}
|
||||
|
||||
private static Pattern messageRegex = Pattern.compile("(?<number>\\d+)(?<unit>[ywdhms]+)");
|
||||
private static List<String> validDuration = Arrays.asList("w", "d", "h", "m", "s");
|
||||
|
||||
public static Duration parseDuration(String textToParseFrom) {
|
||||
Matcher matcher = ParseUtils.messageRegex.matcher(textToParseFrom);
|
||||
@@ -30,11 +33,11 @@ public class ParseUtils {
|
||||
case "h": start = start.plus(Duration.ofHours(parsed)); break;
|
||||
case "m": start = start.plus(Duration.ofMinutes(parsed)); break;
|
||||
case "s": start = start.plus(Duration.ofSeconds(parsed)); break;
|
||||
default: throw new AbstractoRunTimeException(String.format("Invalid time format %s", unit));
|
||||
default: throw new DurationFormatException(unit, validDuration);
|
||||
}
|
||||
}
|
||||
if(!rest.equals("")) {
|
||||
throw new AbstractoRunTimeException(String.format("Invalid time format found: %s", rest));
|
||||
throw new DurationFormatException(rest, validDuration);
|
||||
}
|
||||
return start;
|
||||
}
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
The category ${categoryId?c} was not found in guild ${guildId?c}.
|
||||
@@ -0,0 +1 @@
|
||||
Invalid time format: ${format}. Valid formats are ${valid}. Beware to not use a space between the number and the unit.
|
||||
@@ -0,0 +1 @@
|
||||
Guild ${id} was not found.
|
||||
Reference in New Issue
Block a user