Skip to content

Commit 6da263a

Browse files
committed
Correcting the retry-able try-catch block placement: it must be the topmost in the retry loop, enclosing everything
1 parent 69e2faa commit 6da263a

1 file changed

Lines changed: 21 additions & 17 deletions

File tree

src/Api.cpp

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2503,27 +2503,31 @@ boost::property_tree::ptree Api::sendRequest(const std::string& method, const st
25032503
int retries = 0;
25042504
while (1)
25052505
{
2506-
std::string serverResponse = _httpClient.makeRequest(url, args);
2507-
if (!serverResponse.compare(0, 6, "<html>")) {
2508-
throw TgException("tgbot-cpp library have got html page instead of json response. Maybe you entered wrong bot token.");
2509-
}
2510-
2511-
boost::property_tree::ptree result = _tgTypeParser.parseJson(serverResponse);
25122506
try {
2513-
if (result.get<bool>("ok", false)) {
2514-
return result.get_child("result");
2515-
} else {
2516-
if (retries == _httpClient.getRequestMaxRetries()) {
2507+
std::string serverResponse = _httpClient.makeRequest(url, args);
2508+
if (!serverResponse.compare(0, 6, "<html>")) {
2509+
throw TgException("tgbot-cpp library have got html page instead of json response. Maybe you entered wrong bot token.");
2510+
}
2511+
2512+
boost::property_tree::ptree result = _tgTypeParser.parseJson(serverResponse);
2513+
try {
2514+
if (result.get<bool>("ok", false)) {
2515+
return result.get_child("result");
2516+
} else {
25172517
throw TgException(result.get("description", ""));
25182518
}
2519-
else {
2520-
std::this_thread::sleep_for(std::chrono::seconds(requestRetryBackoff));
2521-
retries++;
2522-
continue;
2523-
}
2519+
} catch (boost::property_tree::ptree_error& e) {
2520+
throw TgException("tgbot-cpp library can't parse json response. " + std::string(e.what()));
2521+
}
2522+
} catch (...) {
2523+
int max_retries = _httpClient.getRequestMaxRetries();
2524+
if ((max_retries >= 0) && (retries == max_retries)) {
2525+
throw;
2526+
} else {
2527+
std::this_thread::sleep_for(std::chrono::seconds(requestRetryBackoff));
2528+
retries++;
2529+
continue;
25242530
}
2525-
} catch (boost::property_tree::ptree_error& e) {
2526-
throw TgException("tgbot-cpp library can't parse json response. " + std::string(e.what()));
25272531
}
25282532
}
25292533
}

0 commit comments

Comments
 (0)