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.
This commit is contained in:
Aikar
2018-06-18 21:31:46 -04:00
parent 46edb06f9b
commit 9aac7d7809
2 changed files with 29 additions and 2 deletions
@@ -70,6 +70,7 @@ public abstract class CommandManager <
protected final CommandReplacements replacements = new CommandReplacements(this);
protected final CommandConditions<I, CEC, CC> conditions = new CommandConditions<>(this);
protected ExceptionHandler defaultExceptionHandler = null;
boolean logUnhandledExceptions = true;
protected Table<Class<?>, 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.
* <p>
* 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.
*
@@ -179,7 +179,10 @@ public class RegisteredCommand <CEC extends CommandExecutionContext<CEC, ? exten
if (!this.manager.handleUncaughtException(scope, this, sender, args, e)) {
sender.sendMessage(MessageType.ERROR, MessageKeys.ERROR_PERFORMING_COMMAND);
}
this.manager.log(LogLevel.ERROR, "Exception in command: " + command + " " + ACFUtil.join(args), e);
boolean hasExceptionHandler = this.manager.defaultExceptionHandler != null || this.scope.getExceptionHandler() != null;
if (!hasExceptionHandler || this.manager.logUnhandledExceptions) {
this.manager.log(LogLevel.ERROR, "Exception in command: " + command + " " + ACFUtil.join(args), e);
}
} catch (Exception e2) {
this.manager.log(LogLevel.ERROR, "Exception in handleException for command: " + command + " " + ACFUtil.join(args), e);
this.manager.log(LogLevel.ERROR, "Exception triggered by exception handler:", e2);