Handle unexpected exceptions better at startup

This commit is contained in:
filoghost 2020-08-07 10:15:30 +02:00
parent 4a5c06398f
commit 36bb378e69
3 changed files with 34 additions and 18 deletions

View File

@ -14,6 +14,7 @@
*/
package me.filoghost.chestcommands;
import me.filoghost.chestcommands.util.Utils;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.plugin.java.JavaPlugin;
@ -28,29 +29,42 @@ public abstract class BaseJavaPlugin extends JavaPlugin {
try {
onCheckedEnable();
} catch (PluginEnableException e) {
criticalShutdown(e.getMessage());
criticalShutdown(e.getMessage(), null);
} catch (Throwable t) {
criticalShutdown(null, t);
}
}
protected abstract void onCheckedEnable() throws PluginEnableException;
private void criticalShutdown(String errorMessage) {
Bukkit.getConsoleSender().sendMessage(getFatalErrorPrefix() + " " + errorMessage);
private void criticalShutdown(String errorMessage, Throwable throwable) {
Bukkit.getConsoleSender().sendMessage(getErrorMessage(errorMessage, throwable));
Bukkit.getScheduler().runTaskLater(this, () -> {
Bukkit.getConsoleSender().sendMessage(getPostStartupMessage(errorMessage));
Bukkit.getConsoleSender().sendMessage(
getFatalErrorPrefix() + "Fatal error while enabling the plugin. Check previous logs for more information.");
}, 10);
setEnabled(false);
}
protected String getPostStartupMessage(String errorMessage) {
protected String getErrorMessage(String errorMessage, Throwable throwable) {
List<String> output = new ArrayList<>();
output.add(getFatalErrorPrefix());
if (throwable != null) {
output.add(getFatalErrorPrefix() + "Fatal unexpected error while enabling plugin:");
} else {
output.add(getFatalErrorPrefix() + "Fatal error while enabling plugin:");
}
if (throwable != null) {
output.add(" ");
output.add(Utils.getStackTraceString(throwable));
}
output.add(" ");
output.add(errorMessage);
if (errorMessage != null) {
output.add(errorMessage);
}
output.add("The plugin has been disabled.");
output.add(" ");
@ -58,7 +72,7 @@ public abstract class BaseJavaPlugin extends JavaPlugin {
}
private String getFatalErrorPrefix() {
return ChatColor.DARK_RED + "[" + getDescription().getName() + "] " + ChatColor.RED + "Fatal error while enabling plugin:";
return ChatColor.DARK_RED + "[" + getDescription().getName() + "] " + ChatColor.RED;
}

View File

@ -20,13 +20,12 @@ import me.filoghost.chestcommands.config.framework.exception.ConfigSyntaxExcepti
import me.filoghost.chestcommands.legacy.UpgradeExecutorException;
import me.filoghost.chestcommands.legacy.upgrade.UpgradeTaskException;
import me.filoghost.chestcommands.parsing.ParseException;
import me.filoghost.chestcommands.util.Utils;
import me.filoghost.chestcommands.util.logging.ErrorCollector;
import me.filoghost.chestcommands.util.logging.ErrorInfo;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
@ -77,7 +76,7 @@ public class PrintableErrorCollector extends ErrorCollector {
}
}
private void printError(StringBuilder output, ErrorPrintInfo error) {
private static void printError(StringBuilder output, ErrorPrintInfo error) {
output.append(ChatColor.YELLOW).append(error.getIndex()).append(") ");
output.append(ChatColor.WHITE).append(MessagePartJoiner.join(error.getMessage()));
@ -90,17 +89,11 @@ public class PrintableErrorCollector extends ErrorCollector {
if (error.getCause() != null) {
output.append(ChatColor.DARK_GRAY);
output.append("--------[ Exception details ]--------\n");
output.append(getStackTraceString(error.getCause()));
output.append(Utils.getStackTraceString(error.getCause()));
output.append("-------------------------------------\n");
}
output.append(" \n");
output.append(ChatColor.RESET);
}
private String getStackTraceString(Throwable throwable) {
StringWriter stringWriter = new StringWriter();
throwable.printStackTrace(new PrintWriter(stringWriter));
return stringWriter.toString();
}
}

View File

@ -14,6 +14,9 @@
*/
package me.filoghost.chestcommands.util;
import java.io.PrintWriter;
import java.io.StringWriter;
public class Utils {
public static boolean isClassLoaded(String name) {
@ -40,4 +43,10 @@ public class Utils {
}
}
public static String getStackTraceString(Throwable throwable) {
StringWriter stringWriter = new StringWriter();
throwable.printStackTrace(new PrintWriter(stringWriter));
return stringWriter.toString();
}
}