From fc1bf1339e4d3e94ccdbb5d68d94b26e930aeaa6 Mon Sep 17 00:00:00 2001 From: Jerry Date: Sat, 20 May 2023 17:12:13 +0800 Subject: [PATCH] error handling --- src/main/java/de/strifel/VTools/VTools.java | 2 ++ .../de/strifel/VTools/listeners/TGBridge.java | 24 +++++++++++++++---- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/main/java/de/strifel/VTools/VTools.java b/src/main/java/de/strifel/VTools/VTools.java index d4e89ff..e3163b1 100644 --- a/src/main/java/de/strifel/VTools/VTools.java +++ b/src/main/java/de/strifel/VTools/VTools.java @@ -16,6 +16,7 @@ import java.nio.file.Path; @Plugin(id = "vtools", name="VTools", version="1.0-SNAPSHOT", description="Some commands!", authors="unnamed") public class VTools { private final ProxyServer server; + public final Logger logger; public final Path dataDirectory; public static final TextColor COLOR_RED = TextColor.fromCSSHexString("#FF5555"); @@ -25,6 +26,7 @@ public class VTools { @Inject public VTools(ProxyServer server, Logger logger, @DataDirectory Path dataDirectory) { this.server = server; + this.logger = logger; this.dataDirectory = dataDirectory; } diff --git a/src/main/java/de/strifel/VTools/listeners/TGBridge.java b/src/main/java/de/strifel/VTools/listeners/TGBridge.java index 4891cc9..3a54e71 100644 --- a/src/main/java/de/strifel/VTools/listeners/TGBridge.java +++ b/src/main/java/de/strifel/VTools/listeners/TGBridge.java @@ -32,6 +32,8 @@ public class TGBridge { private String TOKEN = ""; private long CHAT_ID = 0L; + private long backoffSec = 1L; + public TGBridge(VTools plugin) { INSTANCE = this; this.plugin = plugin; @@ -61,7 +63,7 @@ public class TGBridge { this.TOKEN = config.getOrDefault("token", ""); } } catch (Exception e) { - e.printStackTrace(); + plugin.logger.error("parsing config", e); } } @@ -70,6 +72,7 @@ public class TGBridge { if (TOKEN.isEmpty() || CHAT_ID == 0L) return; bot = new TelegramBot(TOKEN); bot.setUpdatesListener(updates -> { + backoffSec = 1L; for (Update update : updates) { try { if (update != null && @@ -84,11 +87,19 @@ public class TGBridge { } } catch (Exception e) { - e.printStackTrace(); + plugin.logger.error("handling update", e); } } return UpdatesListener.CONFIRMED_UPDATES_ALL; - }, Throwable::printStackTrace); + }, (e) -> { + plugin.logger.error("getting update", e); + plugin.logger.error(String.format("waiting %ds before getting another update", backoffSec)); + try { Thread.sleep(backoffSec * 1000); } catch (InterruptedException ignored) {} + backoffSec *= 2L; + if (backoffSec > 3600) { + backoffSec = 3600; + } + }); } protected void inbound(String content) { @@ -100,23 +111,28 @@ public class TGBridge { } protected void outbound(String content) { - if (TOKEN.isEmpty() || CHAT_ID == 0L) return; + if (bot == null) return; if (content.length() > 4000) { content = content.substring(0, 4000); } bot.execute(new SendMessage(CHAT_ID, content), new Callback() { @Override public void onResponse(SendMessage sendMessage, SendResponse sendResponse) { + if (!sendResponse.isOk()) { + plugin.logger.error(String.format("sendMessage error %d: %s", sendResponse.errorCode(), sendResponse.description())); + } } @Override public void onFailure(SendMessage sendMessage, IOException e) { + plugin.logger.error("sending message", e); } }); } @Subscribe public void onProxyShutdown(ProxyShutdownEvent event) { + if (bot == null) return; bot.removeGetUpdatesListener(); bot.shutdown(); }