diff options
author | Dmitry Mikushin <dmitry@kernelgen.org> | 2023-03-09 11:30:17 +0100 |
---|---|---|
committer | Dmitry Mikushin <dmitry@kernelgen.org> | 2023-03-09 11:30:17 +0100 |
commit | 6da263a5cf24503b1c87a7777bc79a2aab9d553b (patch) | |
tree | 07185082b4265737e30e7962fe2e99277922e8c5 | |
parent | 69e2faa1828eb9c73b592faa69a64ea6c98ec6cf (diff) |
Correcting the retry-able try-catch block placement: it must be the topmost in the retry loop, enclosing everything
-rw-r--r-- | src/Api.cpp | 38 |
1 files changed, 21 insertions, 17 deletions
diff --git a/src/Api.cpp b/src/Api.cpp index b6dba9a..202fb73 100644 --- a/src/Api.cpp +++ b/src/Api.cpp @@ -2503,27 +2503,31 @@ boost::property_tree::ptree Api::sendRequest(const std::string& method, const st int retries = 0; while (1) { - std::string serverResponse = _httpClient.makeRequest(url, args); - if (!serverResponse.compare(0, 6, "<html>")) { - throw TgException("tgbot-cpp library have got html page instead of json response. Maybe you entered wrong bot token."); - } - - boost::property_tree::ptree result = _tgTypeParser.parseJson(serverResponse); try { - if (result.get<bool>("ok", false)) { - return result.get_child("result"); - } else { - if (retries == _httpClient.getRequestMaxRetries()) { + std::string serverResponse = _httpClient.makeRequest(url, args); + if (!serverResponse.compare(0, 6, "<html>")) { + throw TgException("tgbot-cpp library have got html page instead of json response. Maybe you entered wrong bot token."); + } + + boost::property_tree::ptree result = _tgTypeParser.parseJson(serverResponse); + try { + if (result.get<bool>("ok", false)) { + return result.get_child("result"); + } else { throw TgException(result.get("description", "")); } - else { - std::this_thread::sleep_for(std::chrono::seconds(requestRetryBackoff)); - retries++; - continue; - } + } catch (boost::property_tree::ptree_error& e) { + throw TgException("tgbot-cpp library can't parse json response. " + std::string(e.what())); + } + } catch (...) { + int max_retries = _httpClient.getRequestMaxRetries(); + if ((max_retries >= 0) && (retries == max_retries)) { + throw; + } else { + std::this_thread::sleep_for(std::chrono::seconds(requestRetryBackoff)); + retries++; + continue; } - } catch (boost::property_tree::ptree_error& e) { - throw TgException("tgbot-cpp library can't parse json response. " + std::string(e.what())); } } } |