Permalink
Browse files

Setup the exception converter

  • Loading branch information...
me4502 committed Dec 18, 2018
1 parent 33ce872 commit a194f96494f436dad2d0f4284896d169e61cf35f
@@ -34,29 +34,24 @@
import com.sk89q.squirrelid.resolver.HttpRepositoryService;
import com.sk89q.squirrelid.resolver.ProfileService;
import com.sk89q.worldedit.extension.platform.Actor;
import com.sk89q.worldedit.util.task.SimpleSupervisor;
import com.sk89q.worldedit.util.task.Supervisor;
import com.sk89q.worldedit.util.task.Task;
import com.sk89q.worldguard.internal.platform.WorldGuardPlatform;
import com.sk89q.worldguard.protection.flags.Flags;
import com.sk89q.worldguard.protection.flags.registry.FlagRegistry;
import com.sk89q.worldguard.protection.flags.registry.SimpleFlagRegistry;
import com.sk89q.worldguard.protection.managers.storage.StorageException;
import com.sk89q.worldguard.protection.util.UnresolvedNamesException;
import com.sk89q.worldguard.util.WorldGuardExceptionConverter;
import com.sk89q.worldguard.util.concurrent.EvenMoreExecutors;
import com.sk89q.worldedit.util.task.SimpleSupervisor;
import com.sk89q.worldedit.util.task.Supervisor;
import com.sk89q.worldedit.util.task.Task;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.annotation.Nullable;

public class WorldGuard {

public static final Logger logger = Logger.getLogger(WorldGuard.class.getCanonicalName());
@@ -68,6 +63,7 @@
private ProfileCache profileCache;
private ProfileService profileService;
private ListeningExecutorService executorService;
private WorldGuardExceptionConverter exceptionConverter = new WorldGuardExceptionConverter(this);

public static WorldGuard getInstance() {
return instance;
@@ -160,6 +156,15 @@ public ProfileCache getProfileCache() {
return profileCache;
}

/**
* Get the exception converter
*
* @return the exception converter
*/
public WorldGuardExceptionConverter getExceptionConverter() {
return exceptionConverter;
}

/**
* Checks to see if the sender is a player, otherwise throw an exception.
*
@@ -175,34 +180,6 @@ public LocalPlayer checkPlayer(Actor sender) throws CommandException {
}
}

/**
* Convert the throwable into a somewhat friendly message.
*
* @param throwable the throwable
* @return a message
*/
public String convertThrowable(@Nullable Throwable throwable) {
if (throwable instanceof NumberFormatException) {
return "Number expected, string received instead.";
} else if (throwable instanceof StorageException) {
WorldGuard.logger.log(Level.WARNING, "Error loading/saving regions", throwable);
return "Region data could not be loaded/saved: " + throwable.getMessage();
} else if (throwable instanceof RejectedExecutionException) {
return "There are currently too many tasks queued to add yours. Use /wg running to list queued and running tasks.";
} else if (throwable instanceof CancellationException) {
return "WorldGuard: Task was cancelled";
} else if (throwable instanceof InterruptedException) {
return "WorldGuard: Task was interrupted";
} else if (throwable instanceof UnresolvedNamesException) {
return throwable.getMessage();
} else if (throwable instanceof CommandException) {
return throwable.getMessage();
} else {
WorldGuard.logger.log(Level.WARNING, "WorldGuard encountered an unexpected error", throwable);
return "WorldGuard: An unexpected error occurred! Please see the server console.";
}
}

/**
* Called when WorldGuard should be disabled.
*/
@@ -0,0 +1,91 @@
/*
* WorldGuard, a suite of tools for Minecraft
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldGuard team and contributors
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.sk89q.worldguard.util;

import static com.google.common.base.Preconditions.checkNotNull;

import com.sk89q.minecraft.util.commands.CommandException;
import com.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.util.command.parametric.ExceptionConverterHelper;
import com.sk89q.worldedit.util.command.parametric.ExceptionMatch;
import com.sk89q.worldguard.WorldGuard;
import com.sk89q.worldguard.protection.managers.storage.StorageException;
import com.sk89q.worldguard.protection.util.UnresolvedNamesException;

import java.util.concurrent.CancellationException;
import java.util.concurrent.RejectedExecutionException;
import java.util.logging.Level;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class WorldGuardExceptionConverter extends ExceptionConverterHelper {

private static final Pattern numberFormat = Pattern.compile("^For input string: \"(.*)\"$");
private final WorldGuard worldGuard;

public WorldGuardExceptionConverter(WorldGuard worldGuard) {
checkNotNull(worldGuard);
this.worldGuard = worldGuard;
}

@ExceptionMatch
public void convert(NumberFormatException e) throws CommandException {
final Matcher matcher = numberFormat.matcher(e.getMessage());

if (matcher.matches()) {
throw new CommandException("Number expected; string \"" + matcher.group(1)
+ "\" given.");
} else {
throw new CommandException("Number expected; string given.");
}
}

@ExceptionMatch
public void convert(StorageException e) throws CommandException {
WorldGuard.logger.log(Level.WARNING, "Error loading/saving regions", e);
throw new CommandException("Region data could not be loaded/saved: " + e.getMessage());
}

@ExceptionMatch
public void convert(RejectedExecutionException e) throws CommandException {
throw new CommandException("There are currently too many tasks queued to add yours. Use /wg running to list queued and running tasks.", e);
}

@ExceptionMatch
public void convert(CancellationException e) throws CommandException {
throw new CommandException("WorldGuard: Task was cancelled.", e);
}

@ExceptionMatch
public void convert(InterruptedException e) throws CommandException {
throw new CommandException("WorldGuard: Task was interrupted.", e);
}

@ExceptionMatch
public void convert(WorldEditException e) throws CommandException {
throw new CommandException(e.getMessage(), e);
}

@ExceptionMatch
public void convert(UnresolvedNamesException e) throws CommandException {
throw new CommandException(e.getMessage(), e);
}

}
@@ -228,7 +228,17 @@ public void onDisable() {
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
try {
Actor actor = wrapCommandSender(sender);
commands.execute(cmd.getName(), args, actor, actor);
try {
commands.execute(cmd.getName(), args, actor, actor);
} catch (Throwable t) {
Throwable next = t;
do {
WorldGuard.getInstance().getExceptionConverter().convert(next);
next = next.getCause();
} while (next != null);

throw t;
}
} catch (CommandPermissionsException e) {
sender.sendMessage(ChatColor.RED + "You don't have permission.");
} catch (MissingNestedCommandException e) {
@@ -237,7 +247,7 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String
sender.sendMessage(ChatColor.RED + e.getMessage());
sender.sendMessage(ChatColor.RED + e.getUsage());
} catch (WrappedCommandException e) {
sender.sendMessage(ChatColor.RED + WorldGuard.getInstance().convertThrowable(e.getCause()));
sender.sendMessage(ChatColor.RED + e.getCause().getMessage());
} catch (CommandException e) {
sender.sendMessage(ChatColor.RED + e.getMessage());
}
@@ -233,7 +233,7 @@ public static void trackParentCause(Metadatable target, Object parent) {
private boolean indirect;

private Builder(int expectedSize) {
this.causes = new ArrayList<Object>(expectedSize);
this.causes = new ArrayList<>(expectedSize);
}

private void addAll(@Nullable Object... element) {
@@ -250,7 +250,7 @@ private void addAll(@Nullable Object... element) {
} else if (o instanceof Projectile) {
addAll(((Projectile) o).getShooter());
} else if (o instanceof Vehicle) {
addAll(((Vehicle) o).getPassenger());
addAll(((Vehicle) o).getPassengers());
} else if (o instanceof Creature && ((Creature) o).getTarget() != null) {
indirect = true;
addAll(((Creature) o).getTarget());
@@ -26,17 +26,13 @@
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.entity.Player;

import java.util.logging.Logger;

import javax.annotation.Nullable;

/**
* Command-related utility methods.
*/
public final class CommandUtils {

private static final Logger log = Logger.getLogger(CommandUtils.class.getCanonicalName());

private CommandUtils() {
}

@@ -23,13 +23,11 @@
import com.sk89q.minecraft.util.commands.CommandContext;
import com.sk89q.minecraft.util.commands.CommandException;
import com.sk89q.minecraft.util.commands.CommandPermissions;
import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldedit.extension.platform.Actor;
import com.sk89q.worldedit.world.World;
import com.sk89q.worldguard.LocalPlayer;
import com.sk89q.worldguard.WorldGuard;
import com.sk89q.worldguard.bukkit.BukkitUtil;
import com.sk89q.worldguard.bukkit.BukkitWorldConfiguration;
import com.sk89q.worldguard.config.ConfigurationManager;
import com.sk89q.worldguard.config.WorldConfiguration;
import org.bukkit.ChatColor;
@@ -386,7 +386,8 @@ public void info(CommandContext args, Actor sender) throws CommandException {

// Send a response message
Futures.addCallback(future,
new Builder(worldGuard, sender)
new Builder(sender)
.exceptionConverter(worldGuard.getExceptionConverter())
.onFailure("Failed to fetch region information")
.build());
}

0 comments on commit a194f96

Please sign in to comment.