Skip to content

Commit 0519eba

Browse files
committed
Repeat the http request on fail, do not crash with an exception. This is a better design, because Telegram end point sometimes may not respond
1 parent 4356f74 commit 0519eba

1 file changed

Lines changed: 20 additions & 12 deletions

File tree

src/Api.cpp

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#include "tgbot/Api.h"
22

3+
#include <chrono>
4+
#include <thread>
5+
36
namespace TgBot {
47

58
Api::Api(std::string token, const HttpClient& httpClient, const std::string& url)
@@ -2496,20 +2499,25 @@ boost::property_tree::ptree Api::sendRequest(const std::string& method, const st
24962499
url += "/";
24972500
url += method;
24982501

2499-
std::string serverResponse = _httpClient.makeRequest(url, args);
2500-
if (!serverResponse.compare(0, 6, "<html>")) {
2501-
throw TgException("tgbot-cpp library have got html page instead of json response. Maybe you entered wrong bot token.");
2502-
}
2502+
while(1)
2503+
{
2504+
std::string serverResponse = _httpClient.makeRequest(url, args);
2505+
if (!serverResponse.compare(0, 6, "<html>")) {
2506+
throw TgException("tgbot-cpp library have got html page instead of json response. Maybe you entered wrong bot token.");
2507+
}
25032508

2504-
boost::property_tree::ptree result = _tgTypeParser.parseJson(serverResponse);
2505-
try {
2506-
if (result.get<bool>("ok", false)) {
2507-
return result.get_child("result");
2508-
} else {
2509-
throw TgException(result.get("description", ""));
2509+
boost::property_tree::ptree result = _tgTypeParser.parseJson(serverResponse);
2510+
try {
2511+
if (result.get<bool>("ok", false)) {
2512+
return result.get_child("result");
2513+
} else {
2514+
std::this_thread::sleep_for(std::chrono::seconds(1));
2515+
continue;
2516+
//throw TgException(result.get("description", ""));
2517+
}
2518+
} catch (boost::property_tree::ptree_error& e) {
2519+
throw TgException("tgbot-cpp library can't parse json response. " + std::string(e.what()));
25102520
}
2511-
} catch (boost::property_tree::ptree_error& e) {
2512-
throw TgException("tgbot-cpp library can't parse json response. " + std::string(e.what()));
25132521
}
25142522
}
25152523
}

0 commit comments

Comments
 (0)