Print errors in the console if exceptions are not handled (#50)

This commit is contained in:
Andrea Cavalli 2021-10-16 20:23:13 +02:00
parent 6010390c37
commit 32e936bee0
2 changed files with 13 additions and 15 deletions

View File

@ -1,6 +1,8 @@
package it.tdlight.client;
import it.tdlight.jni.TdApi;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Interface for incoming responses from TDLib.
@ -12,12 +14,4 @@ public interface GenericResultHandler<T extends TdApi.Object> {
* Callback called when TDLib responds.
*/
void onResult(Result<T> result);
default void onResult(TdApi.Object result) {
onResult(Result.of(result));
}
default void onErrorResult(Throwable exception) {
onResult(Result.ofError(exception));
}
}

View File

@ -27,7 +27,7 @@ import org.slf4j.LoggerFactory;
@SuppressWarnings("unused")
public final class SimpleTelegramClient implements Authenticable {
private static final Logger logger = LoggerFactory.getLogger(SimpleTelegramClient.class);
public static final Logger LOG = LoggerFactory.getLogger(SimpleTelegramClient.class);
static {
try {
@ -39,7 +39,7 @@ public final class SimpleTelegramClient implements Authenticable {
//noinspection deprecation
Log.setVerbosityLevel(1);
} catch (Throwable ex) {
logger.warn("Can't set verbosity level", ex);
LOG.warn("Can't set verbosity level", ex);
}
}
@ -92,7 +92,7 @@ public final class SimpleTelegramClient implements Authenticable {
handled = true;
}
if (!handled) {
logger.warn("An update was not handled, please use addUpdateHandler(handler) before starting the client!");
LOG.warn("An update was not handled, please use addUpdateHandler(handler) before starting the client!");
}
}
@ -103,7 +103,7 @@ public final class SimpleTelegramClient implements Authenticable {
handled = true;
}
if (!handled) {
logger.warn("Error received from Telegram!", ex);
LOG.warn("Error received from Telegram!", ex);
}
}
@ -114,10 +114,14 @@ public final class SimpleTelegramClient implements Authenticable {
handled = true;
}
if (!handled) {
logger.warn("Unhandled exception!", ex);
LOG.warn("Unhandled exception!", ex);
}
}
private void handleResultHandlingException(Throwable ex) {
LOG.error("Failed to handle the request result", ex);
}
@Override
public AuthenticationData getAuthenticationData() {
return authenticationData;
@ -148,7 +152,7 @@ public final class SimpleTelegramClient implements Authenticable {
if (update instanceof TdApi.Update) {
handler.onUpdate((TdApi.Update) update);
} else {
logger.warn("Unknown update type: {}", update);
LOG.warn("Unknown update type: {}", update);
}
});
}
@ -205,7 +209,7 @@ public final class SimpleTelegramClient implements Authenticable {
* Send a function and get the result
*/
public <T extends TdApi.Object> void send(TdApi.Function function, GenericResultHandler<T> resultHandler) {
client.send(function, resultHandler::onResult, resultHandler::onErrorResult);
client.send(function, result -> resultHandler.onResult(Result.of(result)), this::handleResultHandlingException);
}
/**