Improve error logging for placeholders

This commit is contained in:
filoghost 2021-12-19 12:22:14 +01:00
parent 749d1ff5a0
commit 62e3cb563f
3 changed files with 24 additions and 19 deletions

View File

@ -13,19 +13,25 @@ import java.util.Objects;
public class PlaceholderOccurrence {
private final String unparsedContent;
private final PluginName pluginName;
private final PlaceholderIdentifier identifier;
private final String argument;
private final int hashCode; // Cached for performance reasons
private PlaceholderOccurrence(PluginName pluginName, PlaceholderIdentifier identifier, String argument) {
private PlaceholderOccurrence(String unparsedContent, PluginName pluginName, PlaceholderIdentifier identifier, String argument) {
this.unparsedContent = unparsedContent;
this.pluginName = pluginName;
this.identifier = identifier;
this.argument = argument;
this.hashCode = Objects.hash(pluginName, identifier, argument);
}
public String getUnparsedContent() {
return unparsedContent;
}
public @Nullable PluginName getPluginName() {
return pluginName;
}
@ -39,11 +45,11 @@ public class PlaceholderOccurrence {
}
/*
* Valid placeholder formats:
* {identifier}
* {identifier: argument}
* {pluginName/identifier}
* {pluginName/identifier: argument}
* Valid placeholder content formats (delimiters are not included in the content):
* "identifier"
* "identifier: argument"
* "pluginName/identifier"
* "pluginName/identifier: argument"
*
* identifier is required, pluginName and argument are optional
*/
@ -67,7 +73,7 @@ public class PlaceholderOccurrence {
}
PlaceholderIdentifier identifier = new PlaceholderIdentifier(identifierString);
return new PlaceholderOccurrence(pluginName, identifier, argument);
return new PlaceholderOccurrence(placeholderContent, pluginName, identifier, argument);
}
@Override

View File

@ -66,7 +66,7 @@ public class ActivePlaceholderTracker implements PlaceholderReplaceFunction {
}
return activePlaceholder.computeReplacement(player, tickClock.getCurrentTick());
} catch (PlaceholderException e) {
exceptionHandler.handle(e);
exceptionHandler.handle(e, placeholderOccurrence);
return "[Error]";
}
}

View File

@ -7,8 +7,8 @@ package me.filoghost.holographicdisplays.plugin.placeholder.tracking;
import me.filoghost.fcommons.logging.Log;
import me.filoghost.holographicdisplays.plugin.placeholder.PlaceholderException;
import me.filoghost.holographicdisplays.plugin.placeholder.PlaceholderOccurrence;
import me.filoghost.holographicdisplays.plugin.tick.TickClock;
import me.filoghost.holographicdisplays.plugin.placeholder.registry.PlaceholderExpansion;
import java.util.Map;
import java.util.WeakHashMap;
@ -16,28 +16,27 @@ import java.util.WeakHashMap;
class PlaceholderExceptionHandler {
private final TickClock tickClock;
private final Map<PlaceholderExpansion, Long> lastErrorLogByPlaceholderExpansion;
private final Map<String, Long> lastErrorLogByPlaceholderContent;
PlaceholderExceptionHandler(TickClock tickClock) {
this.tickClock = tickClock;
this.lastErrorLogByPlaceholderExpansion = new WeakHashMap<>();
this.lastErrorLogByPlaceholderContent = new WeakHashMap<>();
}
void handle(PlaceholderException exception) {
PlaceholderExpansion placeholderExpansion = exception.getPlaceholderExpansion();
Long lastErrorLog = lastErrorLogByPlaceholderExpansion.get(placeholderExpansion);
void handle(PlaceholderException exception, PlaceholderOccurrence placeholderOccurrence) {
String unparsedContent = placeholderOccurrence.getUnparsedContent();
Long lastErrorLog = lastErrorLogByPlaceholderContent.get(unparsedContent);
long currentTick = tickClock.getCurrentTick();
if (lastErrorLog != null && currentTick - lastErrorLog < 20) {
return; // Avoid spamming the console too frequently
}
lastErrorLogByPlaceholderExpansion.put(placeholderExpansion, currentTick);
lastErrorLogByPlaceholderContent.put(unparsedContent, currentTick);
Log.warning("The placeholder \"" + placeholderExpansion.getIdentifier() + "\""
+ " registered by the plugin " + placeholderExpansion.getPluginName()
+ " generated an exception."
+ " Please contact the author of " + placeholderExpansion.getPluginName(),
Log.warning("The placeholder {" + unparsedContent + "}"
+ " registered by the plugin " + exception.getPlaceholderExpansion().getPluginName()
+ " generated an exception.",
exception.getCause());
}