moved some more exceptions to templates

made more specific exceptions for certain cases
This commit is contained in:
Sheldan
2020-05-13 23:25:52 +02:00
parent cb1abaed7d
commit cf04687f04
17 changed files with 110 additions and 30 deletions

View File

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

View File

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

View File

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

View File

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