Improve error logging for prefix/suffix, add logging placeholder errors to trace

This commit is contained in:
Vankka 2023-07-21 17:03:01 +03:00
parent b7aff53b1d
commit 5da32a3c45
No known key found for this signature in database
GPG Key ID: 6E50CB7A29B96AD0
5 changed files with 40 additions and 9 deletions

View File

@ -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<Object> 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<Object> 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<Object> getExtras() {
return extras;
}

View File

@ -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);
}
}
}

View File

@ -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<PermissionDataProvider.PrefixAndSuffix, CompletableFuture<String>> 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);
}

View File

@ -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<Class<?>, Set<PlaceholderProvider>> classProviders;
private final Set<PlaceholderResultMapper> mappers = new CopyOnWriteArraySet<>();
private final Set<Object> 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:

View File

@ -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();