From 9aac7d780934f8d0c97e72ff97766e2de6994bf1 Mon Sep 17 00:00:00 2001 From: Aikar Date: Mon, 18 Jun 2018 21:31:46 -0400 Subject: [PATCH] Enable ability to disable logging of uncaught exceptions You must pass your own exception handler in order to do this. Implementors need to be sure to log if its not a desired throw or otherwise you will have silent command failures. --- .../co/aikar/commands/CommandManager.java | 26 ++++++++++++++++++- .../co/aikar/commands/RegisteredCommand.java | 5 +++- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/co/aikar/commands/CommandManager.java b/core/src/main/java/co/aikar/commands/CommandManager.java index 5474047d..d15efdbc 100644 --- a/core/src/main/java/co/aikar/commands/CommandManager.java +++ b/core/src/main/java/co/aikar/commands/CommandManager.java @@ -70,6 +70,7 @@ public abstract class CommandManager < protected final CommandReplacements replacements = new CommandReplacements(this); protected final CommandConditions conditions = new CommandConditions<>(this); protected ExceptionHandler defaultExceptionHandler = null; + boolean logUnhandledExceptions = true; protected Table, String, Object> dependencies = new Table<>(); protected CommandHelpFormatter helpFormatter = new CommandHelpFormatter(this); @@ -294,12 +295,35 @@ public abstract class CommandManager < /** * Sets the default {@link ExceptionHandler} that is called when an exception occurs while executing a command, if the command doesn't have it's own exception handler registered. * - * @param exceptionHandler the handler that should handle uncaught exceptions + * @param exceptionHandler the handler that should handle uncaught exceptions. May not be null if logExceptions is false */ public void setDefaultExceptionHandler(ExceptionHandler exceptionHandler) { + if (exceptionHandler == null && !this.logUnhandledExceptions) { + throw new IllegalArgumentException("You may not disable the default exception handler and have logging of unhandled exceptions disabled"); + } defaultExceptionHandler = exceptionHandler; } + /** + * Sets the default {@link ExceptionHandler} that is called when an exception occurs while executing a command, if the command doesn't have it's own exception handler registered, and lets you control if ACF should also log the exception still. + *

+ * If you disable logging, you need to log it yourself in your handler. + * + * @param exceptionHandler the handler that should handle uncaught exceptions. May not be null if logExceptions is false + * @param logExceptions Whether or not to log exceptions. + */ + public void setDefaultExceptionHandler(ExceptionHandler exceptionHandler, boolean logExceptions) { + if (exceptionHandler == null && !logExceptions) { + throw new IllegalArgumentException("You may not disable the default exception handler and have logging of unhandled exceptions disabled"); + } + this.logUnhandledExceptions = logExceptions; + this.defaultExceptionHandler = exceptionHandler; + } + + public boolean isLoggingUnhandledExceptions() { + return this.logUnhandledExceptions; + } + /** * Gets the current default exception handler, might be null. * diff --git a/core/src/main/java/co/aikar/commands/RegisteredCommand.java b/core/src/main/java/co/aikar/commands/RegisteredCommand.java index af72b1d0..8fceea97 100644 --- a/core/src/main/java/co/aikar/commands/RegisteredCommand.java +++ b/core/src/main/java/co/aikar/commands/RegisteredCommand.java @@ -179,7 +179,10 @@ public class RegisteredCommand