From e002f5f1671820da055c60b3b67f63ef46928833 Mon Sep 17 00:00:00 2001 From: yushijinhun Date: Thu, 4 Oct 2018 16:25:51 +0800 Subject: [PATCH] Do not print stacktrace on known exceptions --- .../authlibinjector/AuthlibInjector.java | 13 +++++++------ .../InjectorInitializationException.java | 19 +++++++++++++++++++ .../javaagent/AuthlibInjectorPremain.java | 6 ++++++ 3 files changed, 32 insertions(+), 6 deletions(-) create mode 100644 src/main/java/moe/yushi/authlibinjector/InjectorInitializationException.java diff --git a/src/main/java/moe/yushi/authlibinjector/AuthlibInjector.java b/src/main/java/moe/yushi/authlibinjector/AuthlibInjector.java index ee0452a..e2c09bd 100644 --- a/src/main/java/moe/yushi/authlibinjector/AuthlibInjector.java +++ b/src/main/java/moe/yushi/authlibinjector/AuthlibInjector.java @@ -67,7 +67,7 @@ public final class AuthlibInjector { private static AtomicBoolean booted = new AtomicBoolean(false); - public static void bootstrap(Consumer transformerRegistry) { + public static void bootstrap(Consumer transformerRegistry) throws InjectorInitializationException { if (!booted.compareAndSet(false, true)) { Logging.LAUNCH.info("Already started, skipping"); return; @@ -79,7 +79,8 @@ public final class AuthlibInjector { if (optionalConfig.isPresent()) { transformerRegistry.accept(createTransformer(optionalConfig.get())); } else { - Logging.LAUNCH.warning("No config available"); + Logging.LAUNCH.severe("No config available"); + throw new InjectorInitializationException(); } } @@ -107,7 +108,7 @@ public final class AuthlibInjector { metadataResponse = asString(getURL(apiRoot)); } catch (IOException e) { Logging.CONFIG.severe("Failed to fetch metadata: " + e); - throw new UncheckedIOException(e); + throw new InjectorInitializationException(e); } } else { @@ -118,7 +119,7 @@ public final class AuthlibInjector { Logging.CONFIG.severe("Unable to decode metadata: " + e + "\n" + "Encoded metadata:\n" + prefetched.get()); - throw e; + throw new InjectorInitializationException(e); } } @@ -128,10 +129,10 @@ public final class AuthlibInjector { try { configuration = YggdrasilConfiguration.parse(apiRoot, metadataResponse); } catch (UncheckedIOException e) { - Logging.CONFIG.severe("Unable to parse metadata: " + e + "\n" + Logging.CONFIG.severe("Unable to parse metadata: " + e.getCause() + "\n" + "Raw metadata:\n" + metadataResponse); - throw e; + throw new InjectorInitializationException(e); } Logging.CONFIG.fine("Parsed metadata: " + configuration); return of(configuration); diff --git a/src/main/java/moe/yushi/authlibinjector/InjectorInitializationException.java b/src/main/java/moe/yushi/authlibinjector/InjectorInitializationException.java new file mode 100644 index 0000000..f5c8aa8 --- /dev/null +++ b/src/main/java/moe/yushi/authlibinjector/InjectorInitializationException.java @@ -0,0 +1,19 @@ +package moe.yushi.authlibinjector; + +public class InjectorInitializationException extends RuntimeException { + + public InjectorInitializationException() { + } + + public InjectorInitializationException(String message, Throwable cause) { + super(message, cause); + } + + public InjectorInitializationException(String message) { + super(message); + } + + public InjectorInitializationException(Throwable cause) { + super(cause); + } +} diff --git a/src/main/java/moe/yushi/authlibinjector/javaagent/AuthlibInjectorPremain.java b/src/main/java/moe/yushi/authlibinjector/javaagent/AuthlibInjectorPremain.java index 052c944..eb0e1fe 100644 --- a/src/main/java/moe/yushi/authlibinjector/javaagent/AuthlibInjectorPremain.java +++ b/src/main/java/moe/yushi/authlibinjector/javaagent/AuthlibInjectorPremain.java @@ -6,6 +6,8 @@ import static moe.yushi.authlibinjector.AuthlibInjector.nonTransformablePackages import java.lang.instrument.Instrumentation; import java.util.Arrays; import java.util.logging.Level; + +import moe.yushi.authlibinjector.InjectorInitializationException; import moe.yushi.authlibinjector.util.Logging; public class AuthlibInjectorPremain { @@ -17,6 +19,8 @@ public class AuthlibInjectorPremain { public static void premain(String arg, Instrumentation instrumentation) { try { initInjector(arg, instrumentation, false); + } catch (InjectorInitializationException e) { + System.exit(1); } catch (Throwable e) { Logging.LAUNCH.log(Level.SEVERE, "An exception has occurred, exiting", e); System.exit(1); @@ -27,6 +31,8 @@ public class AuthlibInjectorPremain { try { Logging.LAUNCH.info("Launched from agentmain"); initInjector(arg, instrumentation, true); + } catch (InjectorInitializationException e) { + ; } catch (Throwable e) { Logging.LAUNCH.log(Level.SEVERE, "An exception has occurred", e); }