Skip to content

Commit 76e2e85

Browse files
authored
Merge pull request #366 from reo7sp/fix-tg-exception
Parse Telegram error response parameters
2 parents e5d7bdf + f8b8efc commit 76e2e85

4 files changed

Lines changed: 38 additions & 4 deletions

File tree

‎include/tgbot/TgException.h‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22

3+
#include "tgbot/Types.h"
34
#include "tgbot/export.h"
45

56
#include <stdexcept>
@@ -30,9 +31,15 @@ class TGBOT_API TgException : public std::runtime_error {
3031
InvalidJson = 101
3132
};
3233

33-
TgException(std::string_view description, ErrorCode errorCode);
34+
TgException(std::string_view description, ErrorCode errorCode,
35+
std::shared_ptr<ResponseParameters> parameters = nullptr);
3436

3537
const ErrorCode errorCode;
38+
39+
/**
40+
* @brief Additional details about the error returned by Telegram, if any.
41+
*/
42+
const std::shared_ptr<ResponseParameters> parameters;
3643
};
3744

3845
} // namespace TgBot

‎src/Api.cpp‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,12 @@ nlohmann::json Api::sendRequest(std::string_view method, const std::vector<HttpF
3939
throw TgException(exception.what(), TgException::ErrorCode::InvalidJson);
4040
}
4141
if (!response.value("ok", false)) {
42+
std::shared_ptr<ResponseParameters> parameters;
43+
if (const auto iterator = response.find("parameters"); iterator != response.end()) {
44+
parameters = std::make_shared<ResponseParameters>(iterator->get<ResponseParameters>());
45+
}
4246
throw TgException(response.value("description", "Telegram Bot API request failed"),
43-
static_cast<TgException::ErrorCode>(response.value("error_code", 0)));
47+
static_cast<TgException::ErrorCode>(response.value("error_code", 0)), std::move(parameters));
4448
}
4549

4650
return response.at("result");

‎src/TgException.cpp‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
#include "tgbot/TgException.h"
22

3+
#include <utility>
4+
35
namespace TgBot {
46

5-
TgException::TgException(std::string_view description, ErrorCode errorCode)
7+
TgException::TgException(std::string_view description, ErrorCode errorCode,
8+
std::shared_ptr<ResponseParameters> parameters)
69
: runtime_error(std::string(description))
7-
, errorCode(errorCode) {
10+
, errorCode(errorCode)
11+
, parameters(std::move(parameters)) {
812
}
913

1014
} // namespace TgBot

‎tests/tgbot/Api.cpp‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,25 @@ TEST(Api, GeneratedMethodMapsTelegramErrors) {
6060
EXPECT_TRUE(requestThrows(ErrorCode::InvalidJson, "error_code:101"));
6161
}
6262

63+
TEST(Api, GeneratedMethodPreservesTelegramErrorParameters) {
64+
HttpClientMock httpClient;
65+
httpClient.response
66+
= R"({"ok":false,"error_code":400,"description":"migrated","parameters":{"migrate_to_chat_id":-1001234567890,"retry_after":42}})";
67+
TgBot::Api api("token", httpClient, "url");
68+
69+
try {
70+
api.getMe();
71+
FAIL() << "Expected TgException";
72+
} catch (const TgBot::TgException& exception) {
73+
EXPECT_EQ(exception.errorCode, TgBot::TgException::ErrorCode::BadRequest);
74+
ASSERT_TRUE(exception.parameters);
75+
ASSERT_TRUE(exception.parameters->migrateToChatId);
76+
EXPECT_EQ(*exception.parameters->migrateToChatId, -1001234567890);
77+
ASSERT_TRUE(exception.parameters->retryAfter);
78+
EXPECT_EQ(*exception.parameters->retryAfter, 42);
79+
}
80+
}
81+
6382
TEST(Api, PassesCompleteUrlToHttpClient) {
6483
HttpClientMock httpClient;
6584
httpClient.response = R"({"ok":true,"result":{"id":1,"is_bot":true,"first_name":"bot"}})";

0 commit comments

Comments
 (0)