mirror of
https://github.com/GeyserMC/Rainbow.git
synced 2025-12-19 14:59:16 +00:00
Display toast upon IO exception on client
This commit is contained in:
@@ -4,37 +4,39 @@ import com.mojang.logging.LogUtils;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public final class RainbowIO {
|
||||
private static final Logger LOGGER = LogUtils.getLogger();
|
||||
private static final List<IOExceptionListener> listeners = new ArrayList<>();
|
||||
|
||||
private RainbowIO() {}
|
||||
|
||||
public static <T> T safeIO(IOSupplier<T> supplier, T defaultValue) {
|
||||
try {
|
||||
return supplier.get();
|
||||
} catch (IOException exception) {
|
||||
LOGGER.error("Failed to perform IO operation!", exception);
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
public static <T> Optional<T> safeIO(IOSupplier<T> supplier) {
|
||||
try {
|
||||
return Optional.ofNullable(supplier.get());
|
||||
} catch (IOException exception) {
|
||||
LOGGER.error("Failed to perform IO operation!", exception);
|
||||
listeners.forEach(listener -> listener.error(exception));
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
|
||||
public static <T> T safeIO(IOSupplier<T> supplier, T defaultValue) {
|
||||
return safeIO(supplier).orElse(defaultValue);
|
||||
}
|
||||
|
||||
public static void safeIO(IORunnable runnable) {
|
||||
try {
|
||||
safeIO(() -> {
|
||||
runnable.run();
|
||||
} catch (IOException exception) {
|
||||
LOGGER.error("Failed to perform IO operation!", exception);
|
||||
}
|
||||
return null;
|
||||
});
|
||||
}
|
||||
|
||||
public static void registerExceptionListener(IOExceptionListener listener) {
|
||||
listeners.add(listener);
|
||||
}
|
||||
|
||||
@FunctionalInterface
|
||||
@@ -48,4 +50,10 @@ public final class RainbowIO {
|
||||
|
||||
void run() throws IOException;
|
||||
}
|
||||
|
||||
@FunctionalInterface
|
||||
public interface IOExceptionListener {
|
||||
|
||||
void error(IOException exception);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user