From 4611c327d47b8f2f3caf38e45bcd6f2f59667a24 Mon Sep 17 00:00:00 2001 From: Rsl1122 <24460436+Rsl1122@users.noreply.github.com> Date: Mon, 16 Dec 2019 15:22:26 +0200 Subject: [PATCH] Improved Extension error cases - Extensions can now be disabled even when the creation fails - Extension creation error stack traces are pruned better Affects issues: - #1248, #1246 --- .../ExtensionServiceImplementation.java | 4 +-- .../modules/SystemObjectProvidingModule.java | 9 ++++++ .../implementation/ExtensionRegister.java | 29 +++++++++++++++++-- 3 files changed, 37 insertions(+), 5 deletions(-) diff --git a/Plan/common/src/main/java/com/djrapitops/plan/extension/ExtensionServiceImplementation.java b/Plan/common/src/main/java/com/djrapitops/plan/extension/ExtensionServiceImplementation.java index 9351e1153..d5c93693b 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/extension/ExtensionServiceImplementation.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/extension/ExtensionServiceImplementation.java @@ -86,8 +86,8 @@ public class ExtensionServiceImplementation implements ExtensionService { try { extensionRegister.registerBuiltInExtensions(config.getExtensionSettings().getDisabled()); } catch (IllegalStateException failedToRegisterOne) { - logger.warn("One or more extensions failed to register, see suppressed exceptions."); - errorHandler.log(L.WARN, this.getClass(), failedToRegisterOne); + logger.warn("One or more extensions failed to register, see suppressed exceptions (They can be disabled in Plan config)."); + errorHandler.log(L.WARN, ExtensionService.class, failedToRegisterOne); } } diff --git a/Plan/common/src/main/java/com/djrapitops/plan/modules/SystemObjectProvidingModule.java b/Plan/common/src/main/java/com/djrapitops/plan/modules/SystemObjectProvidingModule.java index ec5efe87a..d5a6ffc4f 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/modules/SystemObjectProvidingModule.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/modules/SystemObjectProvidingModule.java @@ -23,7 +23,9 @@ import com.djrapitops.plan.settings.locale.LocaleSystem; import dagger.Module; import dagger.Provides; +import javax.inject.Named; import javax.inject.Singleton; +import java.util.function.Function; /** * Module for binding object instances found inside other systems. @@ -45,4 +47,11 @@ public class SystemObjectProvidingModule { return config.getExtensionSettings(); } + @Provides + @Singleton + @Named("isExtensionEnabled") + Function provideExtensionEnabledConfigCheck(PlanConfig config) { + return config.getExtensionSettings()::isEnabled; + } + } \ No newline at end of file diff --git a/Plan/extensions/src/main/java/com/djrapitops/plan/extension/implementation/ExtensionRegister.java b/Plan/extensions/src/main/java/com/djrapitops/plan/extension/implementation/ExtensionRegister.java index ef087744c..bdbcb2a49 100644 --- a/Plan/extensions/src/main/java/com/djrapitops/plan/extension/implementation/ExtensionRegister.java +++ b/Plan/extensions/src/main/java/com/djrapitops/plan/extension/implementation/ExtensionRegister.java @@ -24,6 +24,7 @@ import com.djrapitops.plan.extension.NotReadyException; import com.djrapitops.plan.extension.extractor.ExtensionExtractor; import javax.inject.Inject; +import javax.inject.Named; import javax.inject.Singleton; import java.util.Optional; import java.util.Set; @@ -38,13 +39,15 @@ import java.util.function.Function; @Singleton public class ExtensionRegister { + private final Function isExtensionEnabledInConfig; private IllegalStateException registerException; private Set disabledExtensions; private ExtensionService extensionService; @Inject - public ExtensionRegister() { + public ExtensionRegister(@Named("isExtensionEnabled") Function isExtensionEnabledInConfig) { /* Required for dagger injection */ + this.isExtensionEnabledInConfig = isExtensionEnabledInConfig; } public void registerBuiltInExtensions(Set disabledExtensions) { @@ -92,16 +95,36 @@ public class ExtensionRegister { } private void suppressException(Class factory, Throwable e) { + String factoryName = factory.getSimpleName(); + String extensionName = factoryName.replace("ExtensionFactory", ""); + + if (!isExtensionEnabledInConfig.apply(extensionName)) { + return; + } + // Places all exceptions to one exception with plugin information so that they can be reported. if (registerException == null) { registerException = new IllegalStateException("One or more extensions failed to register:"); registerException.setStackTrace(new StackTraceElement[0]); } - IllegalStateException info = new IllegalStateException(factory.getSimpleName() + " ran into exception when creating Extension", e); - info.setStackTrace(new StackTraceElement[0]); + IllegalStateException info = new IllegalStateException(factoryName + " failed to create Extension", e); + removeUselessStackTraces(info); registerException.addSuppressed(info); } + private void removeUselessStackTraces(Throwable t) { + if (t == null) return; + if (t instanceof ReflectiveOperationException + || t instanceof NoClassDefFoundError + || t instanceof NoSuchFieldError + || t instanceof NoSuchMethodError + || t instanceof IllegalStateException + ) { + t.setStackTrace(new StackTraceElement[0]); + } + removeUselessStackTraces(t.getCause()); + } + private void register( T factory, Function> createExtension