added more specific exceptions

This commit is contained in:
Sheldan
2020-05-13 23:46:58 +02:00
parent cf04687f04
commit b591ae497d
12 changed files with 134 additions and 20 deletions

View File

@@ -15,6 +15,7 @@ import dev.sheldan.abstracto.core.service.management.UserInServerService;
import dev.sheldan.abstracto.core.utils.CompletableFutureList; import dev.sheldan.abstracto.core.utils.CompletableFutureList;
import dev.sheldan.abstracto.modmail.config.ModMailFeature; import dev.sheldan.abstracto.modmail.config.ModMailFeature;
import dev.sheldan.abstracto.modmail.config.ModMailLoggingFeature; import dev.sheldan.abstracto.modmail.config.ModMailLoggingFeature;
import dev.sheldan.abstracto.modmail.exception.ModMailThreadNotFoundException;
import dev.sheldan.abstracto.modmail.models.database.*; import dev.sheldan.abstracto.modmail.models.database.*;
import dev.sheldan.abstracto.modmail.models.dto.ServerChoice; import dev.sheldan.abstracto.modmail.models.dto.ServerChoice;
import dev.sheldan.abstracto.modmail.models.template.*; import dev.sheldan.abstracto.modmail.models.template.*;
@@ -309,7 +310,7 @@ public class ModMailThreadServiceBean implements ModMailThreadService {
messageService.addReactionToMessage("readReaction", modMailThread.getServer().getId(), message); messageService.addReactionToMessage("readReaction", modMailThread.getServer().getId(), message);
}); });
} else { } else {
throw new AbstractoRunTimeException(String.format("Could not find mod mail thread with id %s", modMailThreadId)); throw new ModMailThreadNotFoundException(modMailThreadId);
} }
} }
@@ -348,7 +349,7 @@ public class ModMailThreadServiceBean implements ModMailThreadService {
log.error("Failed to notify about mod mail exception.", e); log.error("Failed to notify about mod mail exception.", e);
} }
} else { } else {
throw new AbstractoRunTimeException(String.format("Could not find mod mail thread with id %s", modMailTreadId)); throw new ModMailThreadNotFoundException(modMailTreadId);
} }
} }
@@ -406,7 +407,7 @@ public class ModMailThreadServiceBean implements ModMailThreadService {
sendModMailFailure("modmail_exception_generic", innerModMailThread.getUser(), modMailThreadId, feedBack, e); sendModMailFailure("modmail_exception_generic", innerModMailThread.getUser(), modMailThreadId, feedBack, e);
} }
} else { } else {
throw new AbstractoRunTimeException(String.format("Could not find mod mail thread with id %s", modMailThreadId)); throw new ModMailThreadNotFoundException(modMailThreadId);
} }
}); });
} else { } else {
@@ -446,7 +447,7 @@ public class ModMailThreadServiceBean implements ModMailThreadService {
undoActionService.performActions(undoActions); undoActionService.performActions(undoActions);
}); });
} else { } else {
throw new AbstractoRunTimeException(String.format("Could not find mod mail thread with id %s", modMailThreadId)); throw new ModMailThreadNotFoundException(modMailThreadId);
} }
} }
@@ -476,7 +477,7 @@ public class ModMailThreadServiceBean implements ModMailThreadService {
undoActionService.performActions(undoActions); undoActionService.performActions(undoActions);
} }
} else { } else {
throw new AbstractoRunTimeException(String.format("Could not find mod mail thread with id %s", modMailThreadId)); throw new ModMailThreadNotFoundException(modMailThreadId);
} }
} }
@@ -532,7 +533,7 @@ public class ModMailThreadServiceBean implements ModMailThreadService {
.futures(completableFutures) .futures(completableFutures)
.build(); .build();
} else { } else {
throw new AbstractoRunTimeException(String.format("Could not find mod mail thread with id %s", modMailThreadId)); throw new ModMailThreadNotFoundException(modMailThreadId);
} }
} }
@@ -544,7 +545,7 @@ public class ModMailThreadServiceBean implements ModMailThreadService {
log.info("Setting thread {} to closed in db.", modMailThread.getId()); log.info("Setting thread {} to closed in db.", modMailThread.getId());
modMailThreadManagementService.setModMailThreadState(modMailThread, ModMailThreadState.CLOSED); modMailThreadManagementService.setModMailThreadState(modMailThread, ModMailThreadState.CLOSED);
} else { } else {
throw new AbstractoRunTimeException(String.format("Could not find mod mail thread with id %s", modMailThreadId)); throw new ModMailThreadNotFoundException(modMailThreadId);
} }
} }
@@ -594,7 +595,7 @@ public class ModMailThreadServiceBean implements ModMailThreadService {
return null; return null;
}); });
} else { } else {
throw new AbstractoRunTimeException(String.format("Could not find mod mail thread with id %s", modMailThreadId)); throw new ModMailThreadNotFoundException(modMailThreadId);
} }
} }
@@ -618,7 +619,7 @@ public class ModMailThreadServiceBean implements ModMailThreadService {
self.saveMessageIds(messages, modMailThread, moderator, anonymous, true); self.saveMessageIds(messages, modMailThread, moderator, anonymous, true);
modMailThreadManagementService.setModMailThreadState(modMailThread, ModMailThreadState.MOD_REPLIED); modMailThreadManagementService.setModMailThreadState(modMailThread, ModMailThreadState.MOD_REPLIED);
} else { } else {
throw new AbstractoRunTimeException(String.format("Could not find mod mail thread with id %s", modMailThreadId)); throw new ModMailThreadNotFoundException(modMailThreadId);
} }
} }

View File

@@ -0,0 +1,28 @@
package dev.sheldan.abstracto.modmail.exception;
import dev.sheldan.abstracto.core.exception.AbstractoRunTimeException;
import dev.sheldan.abstracto.templating.Templatable;
import java.util.HashMap;
public class ModMailThreadNotFoundException extends AbstractoRunTimeException implements Templatable {
private Long modMailThreadId;
public ModMailThreadNotFoundException(Long modMailThreadId) {
super("");
this.modMailThreadId = modMailThreadId;
}
@Override
public String getTemplateName() {
return "modmail_cannot_find_modmail_thread_exception_text";
}
@Override
public Object getTemplateModel() {
HashMap<String, Long> params = new HashMap<>();
params.put("id", this.modMailThreadId);
return params;
}
}

View File

@@ -1,6 +1,5 @@
package dev.sheldan.abstracto.utility.service; package dev.sheldan.abstracto.utility.service;
import dev.sheldan.abstracto.core.exception.AbstractoRunTimeException;
import dev.sheldan.abstracto.core.service.ChannelService; import dev.sheldan.abstracto.core.service.ChannelService;
import dev.sheldan.abstracto.core.service.management.ChannelManagementService; import dev.sheldan.abstracto.core.service.management.ChannelManagementService;
import dev.sheldan.abstracto.core.models.AServerAChannelAUser; import dev.sheldan.abstracto.core.models.AServerAChannelAUser;
@@ -12,6 +11,7 @@ import dev.sheldan.abstracto.templating.model.MessageToSend;
import dev.sheldan.abstracto.core.service.BotService; import dev.sheldan.abstracto.core.service.BotService;
import dev.sheldan.abstracto.scheduling.service.SchedulerService; import dev.sheldan.abstracto.scheduling.service.SchedulerService;
import dev.sheldan.abstracto.templating.service.TemplateService; import dev.sheldan.abstracto.templating.service.TemplateService;
import dev.sheldan.abstracto.utility.exception.ReminderNotFoundException;
import dev.sheldan.abstracto.utility.models.database.Reminder; import dev.sheldan.abstracto.utility.models.database.Reminder;
import dev.sheldan.abstracto.utility.models.template.commands.reminder.ExecutedReminderModel; import dev.sheldan.abstracto.utility.models.template.commands.reminder.ExecutedReminderModel;
import dev.sheldan.abstracto.utility.models.template.commands.reminder.ReminderModel; import dev.sheldan.abstracto.utility.models.template.commands.reminder.ReminderModel;
@@ -98,7 +98,7 @@ public class RemindServiceBean implements ReminderService {
@Override @Override
@Transactional @Transactional
public void executeReminder(Long reminderId) { public void executeReminder(Long reminderId) {
Reminder reminderToRemindFor = reminderManagementService.loadReminder(reminderId).orElseThrow(() -> new AbstractoRunTimeException(String.format("Could not find reminder with id %s", reminderId))); Reminder reminderToRemindFor = reminderManagementService.loadReminder(reminderId).orElseThrow(() -> new ReminderNotFoundException(reminderId));
if(reminderToRemindFor.isReminded()) { if(reminderToRemindFor.isReminded()) {
return; return;
} }
@@ -140,7 +140,7 @@ public class RemindServiceBean implements ReminderService {
} }
reminderManagementService.saveReminder(reminder); reminderManagementService.saveReminder(reminder);
} else { } else {
throw new AbstractoRunTimeException("Reminder does not exist, was already reminded or does not belong to you."); throw new ReminderNotFoundException(reminderId);
} }
} }
} }

View File

@@ -1,7 +1,7 @@
package dev.sheldan.abstracto.utility.service; package dev.sheldan.abstracto.utility.service;
import dev.sheldan.abstracto.core.exception.AbstractoRunTimeException;
import dev.sheldan.abstracto.core.exception.ChannelNotFoundException; import dev.sheldan.abstracto.core.exception.ChannelNotFoundException;
import dev.sheldan.abstracto.core.exception.UserInServerNotFoundException;
import dev.sheldan.abstracto.core.service.management.ChannelManagementService; import dev.sheldan.abstracto.core.service.management.ChannelManagementService;
import dev.sheldan.abstracto.core.service.management.PostTargetManagement; import dev.sheldan.abstracto.core.service.management.PostTargetManagement;
import dev.sheldan.abstracto.core.models.AServerAChannelMessage; import dev.sheldan.abstracto.core.models.AServerAChannelMessage;
@@ -103,8 +103,8 @@ public class StarboardServiceBean implements StarboardService {
@Transactional @Transactional
public void persistPost(CachedMessage message, List<Long> userExceptAuthorIds, List<CompletableFuture<Message>> completableFutures, Long starboardChannelId, Long starredUserId, Long userReactingId) { public void persistPost(CachedMessage message, List<Long> userExceptAuthorIds, List<CompletableFuture<Message>> completableFutures, Long starboardChannelId, Long starredUserId, Long userReactingId) {
AUserInAServer innerStarredUser = userInServerManagementService.loadUser(starredUserId).orElseThrow(() -> new AbstractoRunTimeException(String.format("Cannnot find user with id %s", starredUserId))); AUserInAServer innerStarredUser = userInServerManagementService.loadUser(starredUserId).orElseThrow(() -> new UserInServerNotFoundException(starredUserId));
AUserInAServer innerUserReacting = userInServerManagementService.loadUser(userReactingId).orElseThrow(() -> new AbstractoRunTimeException(String.format("Cannnot find user with id %s", userReactingId))); AUserInAServer innerUserReacting = userInServerManagementService.loadUser(userReactingId).orElseThrow(() -> new UserInServerNotFoundException(userReactingId));
try { try {
AChannel starboardChannel = channelManagementService.loadChannel(starboardChannelId).orElseThrow(() -> new ChannelNotFoundException(starboardChannelId, message.getServerId())); AChannel starboardChannel = channelManagementService.loadChannel(starboardChannelId).orElseThrow(() -> new ChannelNotFoundException(starboardChannelId, message.getServerId()));
Message message1 = completableFutures.get(0).get(); Message message1 = completableFutures.get(0).get();
@@ -116,7 +116,7 @@ public class StarboardServiceBean implements StarboardService {
.build(); .build();
StarboardPost starboardPost = starboardPostManagementService.createStarboardPost(message, innerStarredUser, innerUserReacting, aServerAChannelMessage); StarboardPost starboardPost = starboardPostManagementService.createStarboardPost(message, innerStarredUser, innerUserReacting, aServerAChannelMessage);
userExceptAuthorIds.forEach(aLong -> { userExceptAuthorIds.forEach(aLong -> {
AUserInAServer user = userInServerManagementService.loadUser(aLong).orElseThrow(() -> new AbstractoRunTimeException(String.format("Could not find user with id %s", aLong))); AUserInAServer user = userInServerManagementService.loadUser(aLong).orElseThrow(() -> new UserInServerNotFoundException(aLong));
starboardPostReactorManagementService.addReactor(starboardPost, user); starboardPostReactorManagementService.addReactor(starboardPost, user);
}); });
} catch (InterruptedException | ExecutionException e) { } catch (InterruptedException | ExecutionException e) {

View File

@@ -1,6 +1,5 @@
package dev.sheldan.abstracto.utility.service; package dev.sheldan.abstracto.utility.service;
import dev.sheldan.abstracto.core.exception.AbstractoRunTimeException;
import dev.sheldan.abstracto.core.models.database.AUserInAServer; import dev.sheldan.abstracto.core.models.database.AUserInAServer;
import dev.sheldan.abstracto.templating.model.MessageToSend; import dev.sheldan.abstracto.templating.model.MessageToSend;
import dev.sheldan.abstracto.core.service.BotService; import dev.sheldan.abstracto.core.service.BotService;
@@ -8,6 +7,7 @@ import dev.sheldan.abstracto.core.service.MessageService;
import dev.sheldan.abstracto.core.service.PostTargetService; import dev.sheldan.abstracto.core.service.PostTargetService;
import dev.sheldan.abstracto.core.utils.MessageUtils; import dev.sheldan.abstracto.core.utils.MessageUtils;
import dev.sheldan.abstracto.templating.service.TemplateService; import dev.sheldan.abstracto.templating.service.TemplateService;
import dev.sheldan.abstracto.utility.exception.SuggestionNotFoundException;
import dev.sheldan.abstracto.utility.models.database.Suggestion; import dev.sheldan.abstracto.utility.models.database.Suggestion;
import dev.sheldan.abstracto.utility.models.SuggestionState; import dev.sheldan.abstracto.utility.models.SuggestionState;
import dev.sheldan.abstracto.utility.models.template.commands.SuggestionLog; import dev.sheldan.abstracto.utility.models.template.commands.SuggestionLog;
@@ -65,7 +65,7 @@ public class SuggestionServiceBean implements SuggestionService {
if(guildById != null) { if(guildById != null) {
List<CompletableFuture<Message>> completableFutures = postTargetService.sendEmbedInPostTarget(messageToSend, SUGGESTIONS_TARGET, guildId); List<CompletableFuture<Message>> completableFutures = postTargetService.sendEmbedInPostTarget(messageToSend, SUGGESTIONS_TARGET, guildId);
CompletableFuture.allOf(completableFutures.toArray(new CompletableFuture[0])).thenAccept(aVoid -> { CompletableFuture.allOf(completableFutures.toArray(new CompletableFuture[0])).thenAccept(aVoid -> {
Suggestion innerSuggestion = suggestionManagementService.getSuggestion(suggestionId).orElseThrow(() -> new AbstractoRunTimeException(String.format("Could not find suggestion with id %s", suggestionId))); Suggestion innerSuggestion = suggestionManagementService.getSuggestion(suggestionId).orElseThrow(() -> new SuggestionNotFoundException(suggestionId));
try { try {
Message message = completableFutures.get(0).get(); Message message = completableFutures.get(0).get();
suggestionManagementService.setPostedMessage(innerSuggestion, message); suggestionManagementService.setPostedMessage(innerSuggestion, message);
@@ -85,7 +85,7 @@ public class SuggestionServiceBean implements SuggestionService {
@Override @Override
public void acceptSuggestion(Long suggestionId, String text, SuggestionLog suggestionLog) { public void acceptSuggestion(Long suggestionId, String text, SuggestionLog suggestionLog) {
Suggestion suggestion = suggestionManagementService.getSuggestion(suggestionId).orElseThrow(() -> new AbstractoRunTimeException(String.format("Could not find suggestion with id %s", suggestionId))); Suggestion suggestion = suggestionManagementService.getSuggestion(suggestionId).orElseThrow(() -> new SuggestionNotFoundException(suggestionId));
suggestionManagementService.setSuggestionState(suggestion, SuggestionState.ACCEPTED); suggestionManagementService.setSuggestionState(suggestion, SuggestionState.ACCEPTED);
updateSuggestion(text, suggestionLog, suggestion); updateSuggestion(text, suggestionLog, suggestion);
} }
@@ -130,7 +130,7 @@ public class SuggestionServiceBean implements SuggestionService {
@Override @Override
public void rejectSuggestion(Long suggestionId, String text, SuggestionLog log) { public void rejectSuggestion(Long suggestionId, String text, SuggestionLog log) {
Suggestion suggestion = suggestionManagementService.getSuggestion(suggestionId).orElseThrow(() -> new AbstractoRunTimeException(String.format("Could not find suggestion with id %s", suggestionId))); Suggestion suggestion = suggestionManagementService.getSuggestion(suggestionId).orElseThrow(() -> new SuggestionNotFoundException(suggestionId));
suggestionManagementService.setSuggestionState(suggestion, SuggestionState.REJECTED); suggestionManagementService.setSuggestionState(suggestion, SuggestionState.REJECTED);
updateSuggestion(text, log, suggestion); updateSuggestion(text, log, suggestion);
} }

View File

@@ -0,0 +1,27 @@
package dev.sheldan.abstracto.utility.exception;
import dev.sheldan.abstracto.core.exception.AbstractoRunTimeException;
import dev.sheldan.abstracto.templating.Templatable;
import java.util.HashMap;
public class ReminderNotFoundException extends AbstractoRunTimeException implements Templatable {
private Long reminderId;
public ReminderNotFoundException(Long reminderId) {
super("");
this.reminderId = reminderId;
}
@Override
public String getTemplateName() {
return "reminder_does_not_exist_exception";
}
@Override
public Object getTemplateModel() {
HashMap<String, Long> params = new HashMap<>();
params.put("id", this.reminderId);
return params;
}
}

View File

@@ -0,0 +1,27 @@
package dev.sheldan.abstracto.utility.exception;
import dev.sheldan.abstracto.core.exception.AbstractoRunTimeException;
import dev.sheldan.abstracto.templating.Templatable;
import java.util.HashMap;
public class SuggestionNotFoundException extends AbstractoRunTimeException implements Templatable {
private Long suggestionId;
public SuggestionNotFoundException(Long suggestionId) {
super("");
this.suggestionId = suggestionId;
}
@Override
public String getTemplateName() {
return "suggestion_does_not_exist_exception";
}
@Override
public Object getTemplateModel() {
HashMap<String, Long> params = new HashMap<>();
params.put("id", this.suggestionId);
return params;
}
}

View File

@@ -0,0 +1,27 @@
package dev.sheldan.abstracto.core.exception;
import dev.sheldan.abstracto.templating.Templatable;
import java.util.HashMap;
public class UserInServerNotFoundException extends AbstractoRunTimeException implements Templatable {
private Long userInServerId;
public UserInServerNotFoundException(Long userInServerId) {
super("");
this.userInServerId = userInServerId;
}
@Override
public String getTemplateName() {
return "core_user_in_server_not_found_exception";
}
@Override
public Object getTemplateModel() {
HashMap<String, Long> param = new HashMap<>();
param.put("userInServerId", this.userInServerId);
return param;
}
}

View File

@@ -0,0 +1 @@
Reminder ${id} does not exist, was already reminded or does not belong to you.