1
0
Fork 0
forked from mc/VTools

error handling

This commit is contained in:
JerryXiao 2023-05-20 17:12:13 +08:00
parent 0c6bfe6491
commit fc1bf1339e
Signed by untrusted user: Jerry
GPG key ID: 22618F758B5BE2E5
2 changed files with 22 additions and 4 deletions

View file

@ -16,6 +16,7 @@ import java.nio.file.Path;
@Plugin(id = "vtools", name="VTools", version="1.0-SNAPSHOT", description="Some commands!", authors="unnamed") @Plugin(id = "vtools", name="VTools", version="1.0-SNAPSHOT", description="Some commands!", authors="unnamed")
public class VTools { public class VTools {
private final ProxyServer server; private final ProxyServer server;
public final Logger logger;
public final Path dataDirectory; public final Path dataDirectory;
public static final TextColor COLOR_RED = TextColor.fromCSSHexString("#FF5555"); public static final TextColor COLOR_RED = TextColor.fromCSSHexString("#FF5555");
@ -25,6 +26,7 @@ public class VTools {
@Inject @Inject
public VTools(ProxyServer server, Logger logger, @DataDirectory Path dataDirectory) { public VTools(ProxyServer server, Logger logger, @DataDirectory Path dataDirectory) {
this.server = server; this.server = server;
this.logger = logger;
this.dataDirectory = dataDirectory; this.dataDirectory = dataDirectory;
} }

View file

@ -32,6 +32,8 @@ public class TGBridge {
private String TOKEN = ""; private String TOKEN = "";
private long CHAT_ID = 0L; private long CHAT_ID = 0L;
private long backoffSec = 1L;
public TGBridge(VTools plugin) { public TGBridge(VTools plugin) {
INSTANCE = this; INSTANCE = this;
this.plugin = plugin; this.plugin = plugin;
@ -61,7 +63,7 @@ public class TGBridge {
this.TOKEN = config.getOrDefault("token", ""); this.TOKEN = config.getOrDefault("token", "");
} }
} catch (Exception e) { } 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; if (TOKEN.isEmpty() || CHAT_ID == 0L) return;
bot = new TelegramBot(TOKEN); bot = new TelegramBot(TOKEN);
bot.setUpdatesListener(updates -> { bot.setUpdatesListener(updates -> {
backoffSec = 1L;
for (Update update : updates) { for (Update update : updates) {
try { try {
if (update != null && if (update != null &&
@ -84,11 +87,19 @@ public class TGBridge {
} }
} }
catch (Exception e) { catch (Exception e) {
e.printStackTrace(); plugin.logger.error("handling update", e);
} }
} }
return UpdatesListener.CONFIRMED_UPDATES_ALL; 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) { protected void inbound(String content) {
@ -100,23 +111,28 @@ public class TGBridge {
} }
protected void outbound(String content) { protected void outbound(String content) {
if (TOKEN.isEmpty() || CHAT_ID == 0L) return; if (bot == null) return;
if (content.length() > 4000) { if (content.length() > 4000) {
content = content.substring(0, 4000); content = content.substring(0, 4000);
} }
bot.execute(new SendMessage(CHAT_ID, content), new Callback<SendMessage, SendResponse>() { bot.execute(new SendMessage(CHAT_ID, content), new Callback<SendMessage, SendResponse>() {
@Override @Override
public void onResponse(SendMessage sendMessage, SendResponse sendResponse) { public void onResponse(SendMessage sendMessage, SendResponse sendResponse) {
if (!sendResponse.isOk()) {
plugin.logger.error(String.format("sendMessage error %d: %s", sendResponse.errorCode(), sendResponse.description()));
}
} }
@Override @Override
public void onFailure(SendMessage sendMessage, IOException e) { public void onFailure(SendMessage sendMessage, IOException e) {
plugin.logger.error("sending message", e);
} }
}); });
} }
@Subscribe @Subscribe
public void onProxyShutdown(ProxyShutdownEvent event) { public void onProxyShutdown(ProxyShutdownEvent event) {
if (bot == null) return;
bot.removeGetUpdatesListener(); bot.removeGetUpdatesListener();
bot.shutdown(); bot.shutdown();
} }