Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

text-minimessage: Add stacktraces and more detail to legacy errors #727

Merged
merged 1 commit into from Mar 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -124,17 +124,17 @@ public UnaryOperator<Component> postProcessor() {

@Override
public @NotNull ParsingException newException(@NotNull final String message) {
return new ParsingExceptionImpl(message, this.message, null, EMPTY_TOKEN_ARRAY);
return new ParsingExceptionImpl(message, this.message, null, false, EMPTY_TOKEN_ARRAY);
}

@Override
public @NotNull ParsingException newException(final @NotNull String message, final @NotNull ArgumentQueue tags) {
return new ParsingExceptionImpl(message, this.message, tagsToTokens(((ArgumentQueueImpl<?>) tags).args));
return new ParsingExceptionImpl(message, this.message, null, false, tagsToTokens(((ArgumentQueueImpl<?>) tags).args));
}

@Override
public @NotNull ParsingException newException(final @NotNull String message, final @Nullable Throwable cause, final @NotNull ArgumentQueue tags) {
return new ParsingExceptionImpl(message, this.message, cause, tagsToTokens(((ArgumentQueueImpl<?>) tags).args));
return new ParsingExceptionImpl(message, this.message, cause, false, tagsToTokens(((ArgumentQueueImpl<?>) tags).args));
}

private static Token[] tagsToTokens(final List<? extends Tag.Argument> tags) {
Expand Down
Expand Up @@ -40,6 +40,7 @@ public class ParsingExceptionImpl extends ParsingException {

private final String originalText;
private Token @NotNull [] tokens;
private final boolean withStackTrace;

/**
* Create a new parsing exception.
Expand All @@ -57,6 +58,7 @@ public ParsingExceptionImpl(
super(message);
this.tokens = tokens;
this.originalText = originalText;
this.withStackTrace = false;
}

/**
Expand All @@ -65,18 +67,21 @@ public ParsingExceptionImpl(
* @param message the detail message
* @param originalText the original text which was parsed
* @param cause the cause
* @param withStackTrace whether to generate a stacktrace
* @param tokens the token which caused the error
* @since 4.10.0
*/
public ParsingExceptionImpl(
final String message,
final @Nullable String originalText,
final @Nullable Throwable cause,
final boolean withStackTrace,
final @NotNull Token @NotNull ... tokens
) {
super(message, cause);
this.tokens = tokens;
this.originalText = originalText;
this.withStackTrace = withStackTrace;
}

@Override
Expand Down Expand Up @@ -142,7 +147,10 @@ private String arrow() {
}

@Override
public ParsingExceptionImpl fillInStackTrace() { // no stacktrace
public Throwable fillInStackTrace() {
if (this.withStackTrace) {
return super.fillInStackTrace();
}
return this;
}

Expand Down
Expand Up @@ -158,12 +158,21 @@ public static void parseString(final String message, final MatchedTokenConsumer<
final int length = message.length();
for (int i = 0; i < length; i++) {
final int codePoint = message.codePointAt(i);
if (codePoint == '§') {
throw new ParsingExceptionImpl(
"Legacy formatting codes have been detected in a component - this is unsupported behaviour. Please refer to the Adventure documentation (https://docs.adventure.kyori.net) for more information.",
message,
new Token(i, i + 2, TokenType.TEXT)
);
if (codePoint == '§' && i + 1 < length) {
final int nextChar = Character.toLowerCase(message.codePointAt(i + 1));
// Only throw an exception if the next character is actually going to make a legacy color code
if ((nextChar >= '0' && nextChar <= '9')
|| (nextChar >= 'a' && nextChar <= 'f')
|| nextChar == 'r'
|| (nextChar >= 'k' && nextChar <= 'o')) {
throw new ParsingExceptionImpl(
"Legacy formatting codes have been detected in a MiniMessage string - this is unsupported behaviour. Please refer to the Adventure documentation (https://docs.adventure.kyori.net) for more information.",
message,
null,
true,
new Token(i, i + 2, TokenType.TEXT)
);
}
}

if (!Character.isBmpCodePoint(codePoint)) {
Expand Down