WorldGuard/worldguard-core/src/main/java/com/sk89q/worldguard/util/WorldGuardExceptionConverter.java
wizjany d542ba78ff Remove AsyncCommandHelper for new AsyncCommandBuilder.
Helper suffers from race conditions for short-lived tasks, leading to
some poor UX conditions such as errors not propagating to the user
(because the exception handler wasn't attached to the future yet), or
lack of success messages.

This commit replaces that system by a Builder which takes a callable to
begin, and then takes supervisor, delay message, and the success and
failure messages and handlers as parts of the builder. The success and
failure handlers wrap the callable itself before submitting to the
executor so they will always be run. The supervisor and delay are added
as listeners to the future since they aren't required if the task is
sufficiently short-lived (and to maintain compatibility with the classes
which are now in WorldEdit).

The builder also supports Components for success and failure messages,
as well as consumers of the callable's result or exception for better
customization of output, instead of having to rely on adding a callback
to the future.

The future is still returned for certain special usages.
2019-05-12 14:40:09 -04:00

99 lines
3.8 KiB
Java

/*
* 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 com.sk89q.minecraft.util.commands.CommandException;
import com.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.internal.command.exception.ExceptionConverterHelper;
import com.sk89q.worldedit.internal.command.exception.ExceptionMatch;
import com.sk89q.worldedit.util.auth.AuthorizationException;
import com.sk89q.worldedit.util.formatting.component.InvalidComponentException;
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 CommandException newCommandException(String message, Throwable cause) {
return new CommandException(message, cause);
}
@ExceptionMatch
public void convert(NumberFormatException e) throws CommandException {
final Matcher matcher = numberFormat.matcher(e.getMessage());
if (matcher.matches()) {
throw newCommandException("Number expected; string \"" + matcher.group(1)
+ "\" given.", e);
} else {
throw newCommandException("Number expected; string given.", e);
}
}
@ExceptionMatch
public void convert(InvalidComponentException e) throws CommandException {
throw newCommandException(e.getMessage(), e);
}
@ExceptionMatch
public void convert(StorageException e) throws CommandException {
WorldGuard.logger.log(Level.WARNING, "Error loading/saving regions", e);
throw newCommandException("Region data could not be loaded/saved: " + e.getMessage(), e);
}
@ExceptionMatch
public void convert(RejectedExecutionException e) throws CommandException {
throw newCommandException("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 newCommandException("Task was cancelled.", e);
}
@ExceptionMatch
public void convert(InterruptedException e) throws CommandException {
throw newCommandException("Task was interrupted.", e);
}
@ExceptionMatch
public void convert(WorldEditException e) throws CommandException {
throw newCommandException(e.getMessage(), e);
}
@ExceptionMatch
public void convert(UnresolvedNamesException e) throws CommandException {
throw newCommandException(e.getMessage(), e);
}
@ExceptionMatch
public void convert(AuthorizationException e) throws CommandException {
throw newCommandException("You don't have permission to do that.", e);
}
}