From 5da32a3c4560a211d168995a7d934cfa8ca236f4 Mon Sep 17 00:00:00 2001 From: Vankka Date: Fri, 21 Jul 2023 17:03:01 +0300 Subject: [PATCH] Improve error logging for prefix/suffix, add logging placeholder errors to trace --- .../placeholder/PlaceholderLookupResult.java | 20 ++++++++++++++++++- .../provider/SinglePlaceholder.java | 4 ++-- .../permission/util/PermissionUtil.java | 15 +++++++++++--- .../placeholder/PlaceholderServiceImpl.java | 5 +++++ .../AnnotationPlaceholderProvider.java | 5 ++--- 5 files changed, 40 insertions(+), 9 deletions(-) diff --git a/api/src/main/java/com/discordsrv/api/placeholder/PlaceholderLookupResult.java b/api/src/main/java/com/discordsrv/api/placeholder/PlaceholderLookupResult.java index 1a640e41..6f9c5144 100644 --- a/api/src/main/java/com/discordsrv/api/placeholder/PlaceholderLookupResult.java +++ b/api/src/main/java/com/discordsrv/api/placeholder/PlaceholderLookupResult.java @@ -27,7 +27,6 @@ import java.util.Set; public class PlaceholderLookupResult { - public static final PlaceholderLookupResult LOOKUP_FAILED = new PlaceholderLookupResult(Type.LOOKUP_FAILED); public static final PlaceholderLookupResult DATA_NOT_AVAILABLE = new PlaceholderLookupResult(Type.DATA_NOT_AVAILABLE); public static final PlaceholderLookupResult UNKNOWN_PLACEHOLDER = new PlaceholderLookupResult(Type.UNKNOWN_PLACEHOLDER); @@ -39,25 +38,40 @@ public class PlaceholderLookupResult { return new PlaceholderLookupResult(placeholder, extras); } + public static PlaceholderLookupResult lookupFailed(Throwable error) { + return new PlaceholderLookupResult(error); + } + private final Type type; private final Object value; + private final Throwable error; private final Set extras; protected PlaceholderLookupResult(Type type) { this.type = type; this.value = null; + this.error = null; this.extras = null; } protected PlaceholderLookupResult(Object value) { this.type = Type.SUCCESS; this.value = value; + this.error = null; + this.extras = null; + } + + protected PlaceholderLookupResult(Throwable error) { + this.type = Type.LOOKUP_FAILED; + this.value = null; + this.error = error; this.extras = null; } protected PlaceholderLookupResult(String placeholder, Set extras) { this.type = Type.NEW_LOOKUP; this.value = placeholder; + this.error = null; this.extras = extras; } @@ -69,6 +83,10 @@ public class PlaceholderLookupResult { return value; } + public Throwable getError() { + return error; + } + public Set getExtras() { return extras; } diff --git a/api/src/main/java/com/discordsrv/api/placeholder/provider/SinglePlaceholder.java b/api/src/main/java/com/discordsrv/api/placeholder/provider/SinglePlaceholder.java index 52e8f4b1..eba70b0a 100644 --- a/api/src/main/java/com/discordsrv/api/placeholder/provider/SinglePlaceholder.java +++ b/api/src/main/java/com/discordsrv/api/placeholder/provider/SinglePlaceholder.java @@ -30,8 +30,8 @@ public class SinglePlaceholder implements PlaceholderProvider { return PlaceholderLookupResult.success( resultProvider.get() ); - } catch (Throwable ignored) { - return PlaceholderLookupResult.LOOKUP_FAILED; + } catch (Throwable t) { + return PlaceholderLookupResult.lookupFailed(t); } } } diff --git a/common/src/main/java/com/discordsrv/common/permission/util/PermissionUtil.java b/common/src/main/java/com/discordsrv/common/permission/util/PermissionUtil.java index 8933145e..d36d5878 100644 --- a/common/src/main/java/com/discordsrv/common/permission/util/PermissionUtil.java +++ b/common/src/main/java/com/discordsrv/common/permission/util/PermissionUtil.java @@ -25,6 +25,7 @@ import net.kyori.adventure.text.Component; import java.util.UUID; import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; import java.util.function.Function; public final class PermissionUtil { @@ -42,11 +43,11 @@ public final class PermissionUtil { } public static Component getPrefix(DiscordSRV discordSRV, UUID uuid) { - return getLegacy(discordSRV, perm -> perm.getPrefix(uuid)); + return getLegacy(discordSRV, "prefix", perm -> perm.getPrefix(uuid)); } public static Component getSuffix(DiscordSRV discordSRV, UUID uuid) { - return getLegacy(discordSRV, perm -> perm.getSuffix(uuid)); + return getLegacy(discordSRV, "suffix", perm -> perm.getSuffix(uuid)); } private static Component getMeta(DiscordSRV discordSRV, UUID uuid, String metaKey) { @@ -61,6 +62,7 @@ public final class PermissionUtil { private static Component getLegacy( DiscordSRV discordSRV, + String what, Function> legacy ) { PermissionDataProvider.PrefixAndSuffix permission = discordSRV.getModule(PermissionDataProvider.PrefixAndSuffix.class); @@ -68,7 +70,14 @@ public final class PermissionUtil { return null; } - String data = legacy.apply(permission).join(); + String data = null; + try { + data = legacy.apply(permission).get(); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } catch (ExecutionException e) { + discordSRV.logger().error("Failed to lookup " + what, e.getCause()); + } return translate(discordSRV, data); } diff --git a/common/src/main/java/com/discordsrv/common/placeholder/PlaceholderServiceImpl.java b/common/src/main/java/com/discordsrv/common/placeholder/PlaceholderServiceImpl.java index 8ae1c0f7..87393e26 100644 --- a/common/src/main/java/com/discordsrv/common/placeholder/PlaceholderServiceImpl.java +++ b/common/src/main/java/com/discordsrv/common/placeholder/PlaceholderServiceImpl.java @@ -25,6 +25,8 @@ import com.discordsrv.api.placeholder.annotation.Placeholder; import com.discordsrv.api.placeholder.annotation.PlaceholderRemainder; import com.discordsrv.api.placeholder.mapper.PlaceholderResultMapper; import com.discordsrv.common.DiscordSRV; +import com.discordsrv.common.logging.Logger; +import com.discordsrv.common.logging.NamedLogger; import com.discordsrv.common.placeholder.provider.AnnotationPlaceholderProvider; import com.discordsrv.api.placeholder.provider.PlaceholderProvider; import com.github.benmanes.caffeine.cache.CacheLoader; @@ -47,12 +49,14 @@ import java.util.regex.Pattern; public class PlaceholderServiceImpl implements PlaceholderService { private final DiscordSRV discordSRV; + private final Logger logger; private final LoadingCache, Set> classProviders; private final Set mappers = new CopyOnWriteArraySet<>(); private final Set globalContext = new CopyOnWriteArraySet<>(); public PlaceholderServiceImpl(DiscordSRV discordSRV) { this.discordSRV = discordSRV; + this.logger = new NamedLogger(discordSRV, "PLACEHOLDER_SERVICE"); this.classProviders = discordSRV.caffeineBuilder() .expireAfterAccess(10, TimeUnit.MINUTES) .expireAfterWrite(15, TimeUnit.MINUTES) @@ -245,6 +249,7 @@ public class PlaceholderServiceImpl implements PlaceholderService { replacement = "Unavailable"; break; case LOOKUP_FAILED: + logger.trace("Lookup failed", result.getError()); replacement = "Error"; break; case NEW_LOOKUP: diff --git a/common/src/main/java/com/discordsrv/common/placeholder/provider/AnnotationPlaceholderProvider.java b/common/src/main/java/com/discordsrv/common/placeholder/provider/AnnotationPlaceholderProvider.java index b9cbb094..72ca465a 100644 --- a/common/src/main/java/com/discordsrv/common/placeholder/provider/AnnotationPlaceholderProvider.java +++ b/common/src/main/java/com/discordsrv/common/placeholder/provider/AnnotationPlaceholderProvider.java @@ -85,9 +85,8 @@ public class AnnotationPlaceholderProvider implements PlaceholderProvider { assert method != null; result = PlaceholderMethodUtil.lookup(method, instance, context, remainder); } - } catch (Throwable e) { - e.printStackTrace(); - return PlaceholderLookupResult.LOOKUP_FAILED; + } catch (Throwable t) { + return PlaceholderLookupResult.lookupFailed(t); } String reLookup = annotation.relookup();