Skip to content

Commit

Permalink
Better line numbers for TextException
Browse files Browse the repository at this point in the history
Signed-off-by: Ashcon Partovi <[email protected]>
  • Loading branch information
Electroid committed May 1, 2021
1 parent c7409fc commit 959d473
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 125 deletions.
44 changes: 26 additions & 18 deletions core/src/main/java/tc/oc/pgm/PGMPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -383,31 +383,39 @@ private String format(LogRecord record) {
return record.getMessage();
}

final TextException textErr = tryException(TextException.class, thrown);
if (textErr != null) {
return format(null, textErr.getLocalizedMessage(), textErr.getCause());
}

final InvalidXMLException xmlErr = tryException(InvalidXMLException.class, thrown);
final MapException mapErr = tryException(MapException.class, thrown);
final ModuleLoadException moduleErr = tryException(ModuleLoadException.class, thrown);

String location = null;
if (xmlErr != null) {
return format(xmlErr.getFullLocation(), xmlErr.getMessage(), xmlErr.getCause());
location = xmlErr.getFullLocation();
} else if (mapErr != null) {
location = mapErr.getLocation();
} else if (moduleErr != null) {
final Class<? extends Module> module = moduleErr.getModule();
location = (module == null ? ModuleLoadException.class : module).getSimpleName();
}

final MapException mapErr = tryException(MapException.class, thrown);
if (mapErr != null) {
return format(mapErr.getLocation(), mapErr.getMessage(), mapErr.getCause());
}
final TextException textErr = tryException(TextException.class, thrown);

final ModuleLoadException moduleErr = tryException(ModuleLoadException.class, thrown);
if (moduleErr != null) {
final Class<? extends Module> module = moduleErr.getModule();
return format(
(module == null ? ModuleLoadException.class : module).getSimpleName(),
moduleErr.getMessage(),
moduleErr.getCause());
Throwable cause = thrown.getCause();
String message = thrown.getMessage();
if (textErr != null) {
cause = null;
message = textErr.getLocalizedMessage();
} else if (xmlErr != null) {
cause = xmlErr.getCause();
message = xmlErr.getMessage();
} else if (mapErr != null) {
cause = mapErr.getCause();
message = mapErr.getMessage();
} else if (moduleErr != null) {
cause = moduleErr.getCause();
message = moduleErr.getMessage();
}

return null;
return format(location, message, cause);
}

private String format(
Expand Down
4 changes: 4 additions & 0 deletions util/src/main/i18n/templates/misc.properties
Original file line number Diff line number Diff line change
Expand Up @@ -221,5 +221,9 @@ type.team = team

type.settingkey = setting

type.uuid = uuid

type.localdate = (yyyy-mm-dd) date

# {0} = a URL (e.g. "https://oc.tc")
chat.clickLink = More info at {0}
4 changes: 4 additions & 0 deletions util/src/main/java/tc/oc/pgm/util/text/TextException.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ public static TextException unknown(@Nullable Throwable cause) {
return new TextException(cause, null, "error.unknown");
}

public static TextException invalidFormat(String text, Class<?> type) {
return invalidFormat(text, type, null);
}

public static TextException invalidFormat(String text, Class<?> type, @Nullable Throwable cause) {
return invalidFormat(text, type, null, cause);
}
Expand Down
48 changes: 42 additions & 6 deletions util/src/main/java/tc/oc/pgm/util/text/TextParser.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package tc.oc.pgm.util.text;

import static com.google.common.base.Preconditions.checkNotNull;
import static net.kyori.adventure.text.Component.text;
import static tc.oc.pgm.util.text.TextException.invalidFormat;
import static tc.oc.pgm.util.text.TextException.outOfRange;
import static tc.oc.pgm.util.text.TextException.unknown;
Expand All @@ -17,8 +16,11 @@
import java.sql.DriverManager;
import java.sql.SQLException;
import java.time.Duration;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.time.temporal.ChronoUnit;
import java.util.UUID;
import java.util.logging.Level;
import java.util.regex.Pattern;
import net.kyori.adventure.text.Component;
Expand Down Expand Up @@ -56,7 +58,7 @@ public static boolean parseBoolean(String text) throws TextException {
if (YES.matcher(text).matches()) return true;
if (NO.matcher(text).matches()) return false;

throw invalidFormat(text, boolean.class, null);
throw invalidFormat(text, boolean.class);
}

/**
Expand Down Expand Up @@ -225,7 +227,7 @@ public static Vector parseVector3d(String text, Range<Float> rangeXZ, Range<Floa
final String[] components = COMMA.split(text, 3);

if (components.length != (twod ? 2 : 3)) {
throw invalidFormat(text, type, null);
throw invalidFormat(text, type);
}

return new Vector(
Expand Down Expand Up @@ -285,7 +287,7 @@ public static Version parseVersion(String text, Range<Version> range) throws Tex
final int size = components.length;

if (size < 1 || size > 3) {
throw invalidFormat(text, Version.class, null);
throw invalidFormat(text, Version.class);
}

final int major = parseInteger(components[0], NONNEG);
Expand Down Expand Up @@ -365,6 +367,40 @@ public static <E extends Enum<E>> E parseEnum(String text, Class<E> type) throws
return parseEnum(text, type, null, false);
}

/**
* Parses text into a UUID.
*
* @param text The text.
* @return A UUID.
* @throws TextException If the text is invalid.
*/
public static UUID parseUuid(String text) throws TextException {
checkNotNull(text, "cannot parse uuid from null");

try {
return UUID.fromString(text);
} catch (IllegalArgumentException e) {
throw invalidFormat(text, UUID.class, e);
}
}

/**
* Parses text into a date.
*
* @param text The text.
* @return A date.
* @throws TextException If the text is invalid.
*/
public static LocalDate parseDate(String text) throws TextException {
checkNotNull(text, "cannot parse date from null");

try {
return LocalDate.parse(text, DateTimeFormatter.ISO_LOCAL_DATE);
} catch (DateTimeParseException e) {
throw invalidFormat(text, LocalDate.class, e);
}
}

/**
* Parses text into a text component.
*
Expand Down Expand Up @@ -431,7 +467,7 @@ public static URI parseUri(String text) throws TextException {
checkNotNull(text, "cannot parse uri from null");

if (text.trim().isEmpty()) {
throw invalidFormat(text, URI.class, null);
throw invalidFormat(text, URI.class);
}

try {
Expand Down Expand Up @@ -463,7 +499,7 @@ public static Connection parseSqlConnection(String text) throws TextException {
final String scheme = uri.getScheme();
try {
if (scheme == null || scheme.isEmpty()) {
throw invalidFormat(text, URI.class, null);
throw invalidFormat(text, URI.class);
} else if (scheme.startsWith("sqlite")) {
Class.forName("org.sqlite.JDBC");
} else if (scheme.startsWith("mysql")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ protected InvalidXMLException(
this.column = column > 0 ? column : this.node != null ? this.node.getColumn() : 0;
}

public InvalidXMLException(@Nullable Node node, Throwable cause) {
this("", node, cause);
}

public InvalidXMLException(String message, @Nullable Node node, Throwable cause) {
this(message, node, null, null, 0, 0, 0, cause);
}
Expand Down
Loading

0 comments on commit 959d473

Please sign in to comment.