Skip to content

Commit

Permalink
refactor(command): no longer print dynamic loader error details to th…
Browse files Browse the repository at this point in the history
…e game
  • Loading branch information
WakelessSloth56 committed Jun 23, 2022
1 parent ff19119 commit 8acb51e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 38 deletions.
Expand Up @@ -4,8 +4,6 @@
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import org.apache.logging.log4j.Marker;
import org.auioc.mcmod.arnicalib.ArnicaLib;
import org.auioc.mcmod.arnicalib.api.game.command.ExceptionCommandExceptionType;
import org.auioc.mcmod.arnicalib.utils.LogUtil;
import com.mojang.brigadier.Command;
import com.mojang.brigadier.context.CommandContext;
Expand All @@ -16,50 +14,41 @@ public class CommandHandlerDynamicLoader {

private static final Marker MARKER = LogUtil.getMarker(CommandHandlerDynamicLoader.class);

private static final ExceptionCommandExceptionType CLASS_NOT_FOUND_ERROR = ce("class_not_found");
private static final ExceptionCommandExceptionType GET_RUN_METHOD_ERROR = ce("get_method");
private static final ExceptionCommandExceptionType INVOKE_RUN_METHOD_ERROR = ce("invoke_method");
private static final ExceptionCommandExceptionType NOT_CSE_THROWN_ERROR = ce("not_cse_thrown");

public static int run(String className, CommandContext<CommandSourceStack> ctx) throws CommandSyntaxException {
Class<?> clazz;
try {
clazz = Class.forName(className);
} catch (ClassNotFoundException e) {
throw CLASS_NOT_FOUND_ERROR.create(e);
}

Method runMethod;
try {
runMethod = clazz.getMethod("run", CommandContext.class);
} catch (NoSuchMethodException | SecurityException e) {
throw GET_RUN_METHOD_ERROR.create(e, "Failed to get \"run\" method");
_run(className, ctx);
} catch (Throwable e) {
if (e instanceof CommandSyntaxException cse) {
LOGGER.warn(MARKER, "Command handler " + className + " throws a CommandSyntaxException");
throw cse;
}
throw CommandUtils.LOGGABLE_INTERNAL_ERROR.create();
}
return Command.SINGLE_SUCCESS;
}

private static void _run(String className, CommandContext<CommandSourceStack> ctx) throws Throwable {
try {
Class<?> clazz = Class.forName(className);
Method runMethod = clazz.getMethod("run", CommandContext.class);
runMethod.invoke(null, ctx);
} catch (ClassNotFoundException e) {
rethrow(e, "Cannot load class");
} catch (NoSuchMethodException | SecurityException e) {
rethrow(e, "Cannot get \"run\" method");
} catch (IllegalAccessException | IllegalArgumentException e) {
throw INVOKE_RUN_METHOD_ERROR.create(e, "Failed to invoke \"run\" method");
rethrow(e, "Cannot invoke \"run\" method");
} catch (InvocationTargetException e) {
if (e.getTargetException() instanceof CommandSyntaxException cse) {
LOGGER.warn(MARKER, "Command handler " + runMethod.getDeclaringClass().getName() + "#" + runMethod.getName() + " throws a CommandSyntaxException");
throw cse;
}
throw NOT_CSE_THROWN_ERROR.create((Exception) e.getTargetException(), "Command handler throws an exception that is not a CommandSyntaxException");
rethrow(e.getTargetException(), "Command handler throws an exception that is not a CommandSyntaxException");
}

return Command.SINGLE_SUCCESS;
}


private static ExceptionCommandExceptionType ce(String key) {
return new ExceptionCommandExceptionType(
(e) -> TextUtils.I18nText(
ArnicaLib.i18n("command.handler_dynamic_loader.failure." + key),
e.getClass().getSimpleName() + ": " + e.getMessage()
),
LOGGER, MARKER
);
private static void rethrow(Throwable throwable, String message) throws Throwable {
LOGGER.error(MARKER, message, throwable);
throw throwable;
}

}
7 changes: 1 addition & 6 deletions src/main/resources/assets/arnicalib/lang/en_us.json
Expand Up @@ -18,10 +18,5 @@

"arnicalib.command.version.success": "Version: %s (%s)",
"arnicalib.command.version.failure.zero": "§eCould not read the mod version. §7If this is a development environment you can ignore this message",
"arnicalib.command.version.failure.reflection": "Failed while using reflection to get mod version",

"arnicalib.command.handler_dynamic_loader.failure.class_not_found": "Failed to load class: %s",
"arnicalib.command.handler_dynamic_loader.failure.get_method": "Failed to get \"run\" method: %s",
"arnicalib.command.handler_dynamic_loader.failure.invoke_method": "Failed to invoke \"run\" method: %s",
"arnicalib.command.handler_dynamic_loader.failure.not_cse_thrown": "Command handler throws an exception that is not a CommandSyntaxException: %s"
"arnicalib.command.version.failure.reflection": "Failed while using reflection to get mod version"
}

0 comments on commit 8acb51e

Please sign in to comment.