diff --git a/src/main/java/org/telegram/telegrambots/Constants.java b/src/main/java/org/telegram/telegrambots/Constants.java index bac4a237..60371d0f 100644 --- a/src/main/java/org/telegram/telegrambots/Constants.java +++ b/src/main/java/org/telegram/telegrambots/Constants.java @@ -10,4 +10,6 @@ public class Constants { public static final String BASEURL = "https://api.telegram.org/bot"; public static final String RESPONSEFIELDOK = "ok"; public static final String RESPONSEFIELDRESULT = "result"; + public static final String ERRORDESCRIPTIONFIELD = "description"; + public static final String ERRORCODEFIELD = "error_code"; } diff --git a/src/main/java/org/telegram/telegrambots/TelegramApiException.java b/src/main/java/org/telegram/telegrambots/TelegramApiException.java index 84643e66..d9653b9f 100644 --- a/src/main/java/org/telegram/telegrambots/TelegramApiException.java +++ b/src/main/java/org/telegram/telegrambots/TelegramApiException.java @@ -8,12 +8,13 @@ package org.telegram.telegrambots; */ public class TelegramApiException extends Exception { private String apiResponse = null; + private Integer errorCode; public TelegramApiException(String message) { super(message); } - public TelegramApiException(String message, String apiResponse) { + public TelegramApiException(String message, String apiResponse, Integer errorCode) { super(message); this.apiResponse = apiResponse; } @@ -30,8 +31,10 @@ public class TelegramApiException extends Exception { public String toString() { if (apiResponse == null) { return super.toString(); - } else { + } else if (errorCode == null) { return super.toString() + ": " + apiResponse; + } else { + return super.toString() + ": [" + errorCode + "] " + apiResponse; } } } diff --git a/src/main/java/org/telegram/telegrambots/TelegramBotsApi.java b/src/main/java/org/telegram/telegrambots/TelegramBotsApi.java index 23fa3617..17f37ff2 100644 --- a/src/main/java/org/telegram/telegrambots/TelegramBotsApi.java +++ b/src/main/java/org/telegram/telegrambots/TelegramBotsApi.java @@ -22,6 +22,9 @@ import java.io.File; import java.io.IOException; import java.nio.charset.StandardCharsets; +import static org.telegram.telegrambots.Constants.ERRORCODEFIELD; +import static org.telegram.telegrambots.Constants.ERRORDESCRIPTIONFIELD; + /** * @author Ruben Bermudez * @version 1.0 @@ -111,7 +114,7 @@ public class TelegramBotsApi { String responseContent = EntityUtils.toString(buf, StandardCharsets.UTF_8); JSONObject jsonObject = new JSONObject(responseContent); if (!jsonObject.getBoolean(Constants.RESPONSEFIELDOK)) { - throw new TelegramApiException(webHookURL == null ? "Error removing old webhook" : "Error setting webhook", responseContent); + throw new TelegramApiException(webHookURL == null ? "Error removing old webhook" : "Error setting webhook", jsonObject.getString(ERRORDESCRIPTIONFIELD), jsonObject.getInt(ERRORCODEFIELD)); } } } catch (JSONException e) { diff --git a/src/main/java/org/telegram/telegrambots/bots/AbsSender.java b/src/main/java/org/telegram/telegrambots/bots/AbsSender.java index 994ffd6d..75e99ce7 100644 --- a/src/main/java/org/telegram/telegrambots/bots/AbsSender.java +++ b/src/main/java/org/telegram/telegrambots/bots/AbsSender.java @@ -55,6 +55,9 @@ import java.util.List; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; +import static org.telegram.telegrambots.Constants.ERRORCODEFIELD; +import static org.telegram.telegrambots.Constants.ERRORDESCRIPTIONFIELD; + /** * @author Ruben Bermudez * @version 1.0 @@ -433,7 +436,7 @@ public abstract class AbsSender { JSONObject jsonObject = new JSONObject(responseContent); if (!jsonObject.getBoolean(Constants.RESPONSEFIELDOK)) { - throw new TelegramApiException("Error at sendDocument", jsonObject.getString("description")); + throw new TelegramApiException("Error at sendDocument", jsonObject.getString(ERRORDESCRIPTIONFIELD), jsonObject.getInt(ERRORCODEFIELD)); } return new Message(jsonObject.getJSONObject(Constants.RESPONSEFIELDRESULT)); @@ -492,7 +495,7 @@ public abstract class AbsSender { JSONObject jsonObject = new JSONObject(responseContent); if (!jsonObject.getBoolean(Constants.RESPONSEFIELDOK)) { - throw new TelegramApiException("Error at sendPhoto", jsonObject.getString("description")); + throw new TelegramApiException("Error at sendPhoto", jsonObject.getString(ERRORDESCRIPTIONFIELD), jsonObject.getInt(ERRORCODEFIELD)); } return new Message(jsonObject.getJSONObject(Constants.RESPONSEFIELDRESULT)); @@ -569,7 +572,7 @@ public abstract class AbsSender { JSONObject jsonObject = new JSONObject(responseContent); if (!jsonObject.getBoolean(Constants.RESPONSEFIELDOK)) { - throw new TelegramApiException("Error at sendVideo", jsonObject.getString("description")); + throw new TelegramApiException("Error at sendVideo", jsonObject.getString(ERRORDESCRIPTIONFIELD), jsonObject.getInt(ERRORCODEFIELD)); } return new Message(jsonObject.getJSONObject(Constants.RESPONSEFIELDRESULT)); @@ -623,7 +626,7 @@ public abstract class AbsSender { JSONObject jsonObject = new JSONObject(responseContent); if (!jsonObject.getBoolean(Constants.RESPONSEFIELDOK)) { - throw new TelegramApiException("Error at sendSticker", jsonObject.getString("description")); + throw new TelegramApiException("Error at sendSticker", jsonObject.getString(ERRORDESCRIPTIONFIELD), jsonObject.getInt(ERRORCODEFIELD)); } return new Message(jsonObject.getJSONObject(Constants.RESPONSEFIELDRESULT)); @@ -705,7 +708,7 @@ public abstract class AbsSender { * {"description":"[Error]: Bad Request: chat not found","error_code":400,"ok":false} */ if (!jsonObject.getBoolean(Constants.RESPONSEFIELDOK)) { - throw new TelegramApiException("Error at sendAudio", jsonObject.getString("description")); + throw new TelegramApiException("Error at sendAudio", jsonObject.getString(ERRORDESCRIPTIONFIELD), jsonObject.getInt(ERRORCODEFIELD)); } // and if not, we can expect a "result" section. and out of this can a new Message object be built @@ -773,7 +776,7 @@ public abstract class AbsSender { JSONObject jsonObject = new JSONObject(responseContent); if (!jsonObject.getBoolean(Constants.RESPONSEFIELDOK)) { - throw new TelegramApiException("Error at sendVoice", jsonObject.getString("description")); + throw new TelegramApiException("Error at sendVoice", jsonObject.getString(ERRORDESCRIPTIONFIELD), jsonObject.getInt(ERRORCODEFIELD)); } return new Message(jsonObject.getJSONObject(Constants.RESPONSEFIELDRESULT)); @@ -828,7 +831,7 @@ public abstract class AbsSender { JSONObject jsonObject = new JSONObject(responseContent); if (!jsonObject.getBoolean(Constants.RESPONSEFIELDOK)) { - throw new TelegramApiException("Error at " + method.getPath(), jsonObject.getString("description")); + throw new TelegramApiException("Error at " + method.getPath(), jsonObject.getString(ERRORDESCRIPTIONFIELD), jsonObject.getInt(ERRORCODEFIELD)); } return method.deserializeResponse(jsonObject); diff --git a/src/main/java/org/telegram/telegrambots/updatesreceivers/BotSession.java b/src/main/java/org/telegram/telegrambots/updatesreceivers/BotSession.java index 932abb23..c3178b6b 100644 --- a/src/main/java/org/telegram/telegrambots/updatesreceivers/BotSession.java +++ b/src/main/java/org/telegram/telegrambots/updatesreceivers/BotSession.java @@ -16,6 +16,7 @@ import org.json.JSONException; import org.json.JSONObject; import org.telegram.telegrambots.BotLogger; import org.telegram.telegrambots.Constants; +import org.telegram.telegrambots.TelegramApiException; import org.telegram.telegrambots.api.methods.updates.GetUpdates; import org.telegram.telegrambots.api.objects.Update; import org.telegram.telegrambots.bots.ITelegramLongPollingBot; @@ -26,6 +27,9 @@ import java.nio.charset.StandardCharsets; import java.util.concurrent.ConcurrentLinkedDeque; import java.util.concurrent.TimeUnit; +import static org.telegram.telegrambots.Constants.ERRORCODEFIELD; +import static org.telegram.telegrambots.Constants.ERRORDESCRIPTIONFIELD; + /** * @author Ruben Bermudez * @version 1.0 @@ -107,7 +111,7 @@ public class BotSession { String responseContent = EntityUtils.toString(buf, StandardCharsets.UTF_8); JSONObject jsonObject = new JSONObject(responseContent); if (!jsonObject.getBoolean(Constants.RESPONSEFIELDOK)) { - throw new InvalidObjectException(jsonObject.toString()); + throw new TelegramApiException("Error getting updates", jsonObject.getString(ERRORDESCRIPTIONFIELD), jsonObject.getInt(ERRORCODEFIELD)); } JSONArray jsonArray = jsonObject.getJSONArray(Constants.RESPONSEFIELDRESULT); if (jsonArray.length() != 0) { @@ -130,7 +134,7 @@ public class BotSession { BotLogger.severe(LOGTAG, e); } } - } catch (InvalidObjectException | JSONException e) { + } catch (InvalidObjectException | JSONException | TelegramApiException e) { BotLogger.severe(LOGTAG, e); } } catch (Exception global) {