Improve telegramapiexceptions
This commit is contained in:
parent
0f13e22917
commit
f1ce1a6ee9
@ -10,4 +10,6 @@ public class Constants {
|
|||||||
public static final String BASEURL = "https://api.telegram.org/bot";
|
public static final String BASEURL = "https://api.telegram.org/bot";
|
||||||
public static final String RESPONSEFIELDOK = "ok";
|
public static final String RESPONSEFIELDOK = "ok";
|
||||||
public static final String RESPONSEFIELDRESULT = "result";
|
public static final String RESPONSEFIELDRESULT = "result";
|
||||||
|
public static final String ERRORDESCRIPTIONFIELD = "description";
|
||||||
|
public static final String ERRORCODEFIELD = "error_code";
|
||||||
}
|
}
|
||||||
|
@ -8,12 +8,13 @@ package org.telegram.telegrambots;
|
|||||||
*/
|
*/
|
||||||
public class TelegramApiException extends Exception {
|
public class TelegramApiException extends Exception {
|
||||||
private String apiResponse = null;
|
private String apiResponse = null;
|
||||||
|
private Integer errorCode;
|
||||||
|
|
||||||
public TelegramApiException(String message) {
|
public TelegramApiException(String message) {
|
||||||
super(message);
|
super(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
public TelegramApiException(String message, String apiResponse) {
|
public TelegramApiException(String message, String apiResponse, Integer errorCode) {
|
||||||
super(message);
|
super(message);
|
||||||
this.apiResponse = apiResponse;
|
this.apiResponse = apiResponse;
|
||||||
}
|
}
|
||||||
@ -30,8 +31,10 @@ public class TelegramApiException extends Exception {
|
|||||||
public String toString() {
|
public String toString() {
|
||||||
if (apiResponse == null) {
|
if (apiResponse == null) {
|
||||||
return super.toString();
|
return super.toString();
|
||||||
} else {
|
} else if (errorCode == null) {
|
||||||
return super.toString() + ": " + apiResponse;
|
return super.toString() + ": " + apiResponse;
|
||||||
|
} else {
|
||||||
|
return super.toString() + ": [" + errorCode + "] " + apiResponse;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -22,6 +22,9 @@ import java.io.File;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
|
|
||||||
|
import static org.telegram.telegrambots.Constants.ERRORCODEFIELD;
|
||||||
|
import static org.telegram.telegrambots.Constants.ERRORDESCRIPTIONFIELD;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Ruben Bermudez
|
* @author Ruben Bermudez
|
||||||
* @version 1.0
|
* @version 1.0
|
||||||
@ -111,7 +114,7 @@ public class TelegramBotsApi {
|
|||||||
String responseContent = EntityUtils.toString(buf, StandardCharsets.UTF_8);
|
String responseContent = EntityUtils.toString(buf, StandardCharsets.UTF_8);
|
||||||
JSONObject jsonObject = new JSONObject(responseContent);
|
JSONObject jsonObject = new JSONObject(responseContent);
|
||||||
if (!jsonObject.getBoolean(Constants.RESPONSEFIELDOK)) {
|
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) {
|
} catch (JSONException e) {
|
||||||
|
@ -55,6 +55,9 @@ import java.util.List;
|
|||||||
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.ExecutorService;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
|
|
||||||
|
import static org.telegram.telegrambots.Constants.ERRORCODEFIELD;
|
||||||
|
import static org.telegram.telegrambots.Constants.ERRORDESCRIPTIONFIELD;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Ruben Bermudez
|
* @author Ruben Bermudez
|
||||||
* @version 1.0
|
* @version 1.0
|
||||||
@ -433,7 +436,7 @@ public abstract class AbsSender {
|
|||||||
|
|
||||||
JSONObject jsonObject = new JSONObject(responseContent);
|
JSONObject jsonObject = new JSONObject(responseContent);
|
||||||
if (!jsonObject.getBoolean(Constants.RESPONSEFIELDOK)) {
|
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));
|
return new Message(jsonObject.getJSONObject(Constants.RESPONSEFIELDRESULT));
|
||||||
@ -492,7 +495,7 @@ public abstract class AbsSender {
|
|||||||
|
|
||||||
JSONObject jsonObject = new JSONObject(responseContent);
|
JSONObject jsonObject = new JSONObject(responseContent);
|
||||||
if (!jsonObject.getBoolean(Constants.RESPONSEFIELDOK)) {
|
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));
|
return new Message(jsonObject.getJSONObject(Constants.RESPONSEFIELDRESULT));
|
||||||
@ -569,7 +572,7 @@ public abstract class AbsSender {
|
|||||||
|
|
||||||
JSONObject jsonObject = new JSONObject(responseContent);
|
JSONObject jsonObject = new JSONObject(responseContent);
|
||||||
if (!jsonObject.getBoolean(Constants.RESPONSEFIELDOK)) {
|
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));
|
return new Message(jsonObject.getJSONObject(Constants.RESPONSEFIELDRESULT));
|
||||||
@ -623,7 +626,7 @@ public abstract class AbsSender {
|
|||||||
|
|
||||||
JSONObject jsonObject = new JSONObject(responseContent);
|
JSONObject jsonObject = new JSONObject(responseContent);
|
||||||
if (!jsonObject.getBoolean(Constants.RESPONSEFIELDOK)) {
|
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));
|
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}
|
* {"description":"[Error]: Bad Request: chat not found","error_code":400,"ok":false}
|
||||||
*/
|
*/
|
||||||
if (!jsonObject.getBoolean(Constants.RESPONSEFIELDOK)) {
|
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
|
// 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);
|
JSONObject jsonObject = new JSONObject(responseContent);
|
||||||
if (!jsonObject.getBoolean(Constants.RESPONSEFIELDOK)) {
|
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));
|
return new Message(jsonObject.getJSONObject(Constants.RESPONSEFIELDRESULT));
|
||||||
@ -828,7 +831,7 @@ public abstract class AbsSender {
|
|||||||
|
|
||||||
JSONObject jsonObject = new JSONObject(responseContent);
|
JSONObject jsonObject = new JSONObject(responseContent);
|
||||||
if (!jsonObject.getBoolean(Constants.RESPONSEFIELDOK)) {
|
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);
|
return method.deserializeResponse(jsonObject);
|
||||||
|
@ -16,6 +16,7 @@ import org.json.JSONException;
|
|||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
import org.telegram.telegrambots.BotLogger;
|
import org.telegram.telegrambots.BotLogger;
|
||||||
import org.telegram.telegrambots.Constants;
|
import org.telegram.telegrambots.Constants;
|
||||||
|
import org.telegram.telegrambots.TelegramApiException;
|
||||||
import org.telegram.telegrambots.api.methods.updates.GetUpdates;
|
import org.telegram.telegrambots.api.methods.updates.GetUpdates;
|
||||||
import org.telegram.telegrambots.api.objects.Update;
|
import org.telegram.telegrambots.api.objects.Update;
|
||||||
import org.telegram.telegrambots.bots.ITelegramLongPollingBot;
|
import org.telegram.telegrambots.bots.ITelegramLongPollingBot;
|
||||||
@ -26,6 +27,9 @@ import java.nio.charset.StandardCharsets;
|
|||||||
import java.util.concurrent.ConcurrentLinkedDeque;
|
import java.util.concurrent.ConcurrentLinkedDeque;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
import static org.telegram.telegrambots.Constants.ERRORCODEFIELD;
|
||||||
|
import static org.telegram.telegrambots.Constants.ERRORDESCRIPTIONFIELD;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Ruben Bermudez
|
* @author Ruben Bermudez
|
||||||
* @version 1.0
|
* @version 1.0
|
||||||
@ -107,7 +111,7 @@ public class BotSession {
|
|||||||
String responseContent = EntityUtils.toString(buf, StandardCharsets.UTF_8);
|
String responseContent = EntityUtils.toString(buf, StandardCharsets.UTF_8);
|
||||||
JSONObject jsonObject = new JSONObject(responseContent);
|
JSONObject jsonObject = new JSONObject(responseContent);
|
||||||
if (!jsonObject.getBoolean(Constants.RESPONSEFIELDOK)) {
|
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);
|
JSONArray jsonArray = jsonObject.getJSONArray(Constants.RESPONSEFIELDRESULT);
|
||||||
if (jsonArray.length() != 0) {
|
if (jsonArray.length() != 0) {
|
||||||
@ -130,7 +134,7 @@ public class BotSession {
|
|||||||
BotLogger.severe(LOGTAG, e);
|
BotLogger.severe(LOGTAG, e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (InvalidObjectException | JSONException e) {
|
} catch (InvalidObjectException | JSONException | TelegramApiException e) {
|
||||||
BotLogger.severe(LOGTAG, e);
|
BotLogger.severe(LOGTAG, e);
|
||||||
}
|
}
|
||||||
} catch (Exception global) {
|
} catch (Exception global) {
|
||||||
|
Loading…
Reference in New Issue
Block a user