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

View File

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

View File

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