|
| 1 | +/* |
| 2 | + * ch.vorburger.minecraft.storeys |
| 3 | + * |
| 4 | + * Copyright (C) 2016 - 2018 Michael Vorburger.ch <mike@vorburger.ch> |
| 5 | + * |
| 6 | + * This program is free software: you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU Affero General Public License as published |
| 8 | + * by the Free Software Foundation, either version 3 of the License, or |
| 9 | + * (at your option) any later version. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + * GNU Affero General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU Affero General Public License |
| 17 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | + */ |
| 19 | +package ch.vorburger.minecraft.storeys.japi.util; |
| 20 | + |
| 21 | +import java.util.concurrent.Callable; |
| 22 | +import org.slf4j.Logger; |
| 23 | +import org.slf4j.LoggerFactory; |
| 24 | +import org.spongepowered.api.command.CommandException; |
| 25 | + |
| 26 | +/** |
| 27 | + * Utilities for {@link CommandException}. |
| 28 | + * |
| 29 | + * @author Michael Vorburger.ch |
| 30 | + */ |
| 31 | +public final class CommandExceptions { |
| 32 | + |
| 33 | + // Copy/pasted from https://github.com/vorburger/ch.vorburger.minecraft.osgi/blob/master/ch.vorburger.minecraft.osgi.api/src/main/java/ch/vorburger/minecraft/utils/CommandExceptions.java |
| 34 | + |
| 35 | + private static final Logger LOG = LoggerFactory.getLogger(CommandExceptions.class); |
| 36 | + |
| 37 | + private CommandExceptions() { |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Invoke 'callable' and return its value, |
| 42 | + * or rethrow any Exception from it wrapped in a CommandException, |
| 43 | + * with description. |
| 44 | + * |
| 45 | + * @param description a humand-readable description of the Callable (used in the CommandException) |
| 46 | + * @param callable the code to invoke |
| 47 | + * @return the value returned by the callable |
| 48 | + * @throws CommandException in case the callable failed with an Exception |
| 49 | + */ |
| 50 | + public static <T> T getOrThrow(String description, Callable<T> callable) throws CommandException { |
| 51 | + try { |
| 52 | + return callable.call(); |
| 53 | + } catch (Exception cause) { |
| 54 | + // TODO see isDeveloper() idea in Texts.fromThrowable |
| 55 | + throw new CommandException(Texts.fromThrowable(description, cause), cause, true); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + public static void doOrThrow(String description, RunnableWithException runnable) throws CommandException { |
| 60 | + try { |
| 61 | + runnable.run(); |
| 62 | + } catch (Exception cause) { |
| 63 | + // TODO once the cause is properly throw to the user incl. stack trace, this log is probably redundant; then remove? |
| 64 | + LOG.error("doOrThrow()", cause); |
| 65 | + // TODO see isDeveloper() idea in Texts.fromThrowable |
| 66 | + throw new CommandException(Texts.fromThrowable(description, cause), cause, true); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + public static CommandException create(String message) { |
| 71 | + return new CommandException(Texts.inRed(message)); |
| 72 | + } |
| 73 | + |
| 74 | + @FunctionalInterface |
| 75 | + public interface RunnableWithException { |
| 76 | + void run() throws Exception; |
| 77 | + } |
| 78 | + |
| 79 | +} |
0 commit comments