Exception handling in commands

By: Dinnerbone <dinnerbone@dinnerbone.com>
This commit is contained in:
Bukkit/Spigot 2011-02-18 16:25:06 +00:00
parent eb206f49c1
commit 636c1ec83a
4 changed files with 47 additions and 8 deletions

View file

@ -0,0 +1,27 @@
package org.bukkit.command;
/**
* Thrown when an unhandled exception occurs during the execution of a Command
*/
public class CommandException extends RuntimeException {
/**
* Creates a new instance of <code>CommandException</code> without detail message.
*/
public CommandException() {
}
/**
* Constructs an instance of <code>CommandException</code> with the specified detail message.
* @param msg the detail message.
*/
public CommandException(String msg) {
super(msg);
}
public CommandException(String msg, Throwable cause) {
super(msg, cause);
}
}

View file

@ -1,7 +1,6 @@
package org.bukkit.command;
import java.util.List;
import org.bukkit.entity.Player;
public interface CommandMap {
/**
@ -19,10 +18,12 @@ public interface CommandMap {
*/
public boolean register(String label, String fallbackPrefix, Command command);
/** Looks for the requested command and executes it if found.
/**
* Looks for the requested command and executes it if found.
*
* @param cmdLine command + arguments. Example: "/test abc 123"
* @return targetFound returns false if no target is found.
* @param cmdLine command + arguments. Example: "/test abc 123"
* @return targetFound returns false if no target is found.
* @throws CommandException Thrown when the executor for the given command fails with an unhandled exception
*/
public boolean dispatch(CommandSender sender, String cmdLine);

View file

@ -1,7 +1,6 @@
package org.bukkit.command;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
public final class PluginCommand extends Command {
@ -14,7 +13,14 @@ public final class PluginCommand extends Command {
}
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
boolean cmdSuccess = owningPlugin.onCommand(sender, this, commandLabel, args);
boolean cmdSuccess = false;
try {
owningPlugin.onCommand(sender, this, commandLabel, args);
} catch (Throwable ex) {
throw new CommandException("Unhandled exception executing command '" + commandLabel + "' in plugin " + owningPlugin.getDescription().getFullName(), ex);
}
if (!cmdSuccess && !usageMessage.isEmpty()) {
String tmpMsg = usageMessage.replace("<command>", commandLabel);
String[] usageLines = tmpMsg.split("\\n");

View file

@ -8,7 +8,6 @@ import java.util.Map;
import org.bukkit.ChatColor;
import org.bukkit.Server;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginDescriptionFile;
@ -74,7 +73,13 @@ public final class SimpleCommandMap implements CommandMap {
Command target = knownCommands.get(sentCommandLabel);
boolean isRegisteredCommand = (target != null);
if (isRegisteredCommand) {
target.execute(sender, sentCommandLabel, args);
try {
target.execute(sender, sentCommandLabel, args);
} catch (CommandException ex) {
throw ex;
} catch (Throwable ex) {
throw new CommandException("Unhandled exception executing '" + commandLine + "' in " + target, ex);
}
}
return isRegisteredCommand;
}