Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion include/tgbot/TgException.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include "tgbot/Types.h"
#include "tgbot/export.h"

#include <stdexcept>
Expand Down Expand Up @@ -30,9 +31,15 @@ class TGBOT_API TgException : public std::runtime_error {
InvalidJson = 101
};

TgException(std::string_view description, ErrorCode errorCode);
TgException(std::string_view description, ErrorCode errorCode,
std::shared_ptr<ResponseParameters> parameters = nullptr);

const ErrorCode errorCode;

/**
* @brief Additional details about the error returned by Telegram, if any.
*/
const std::shared_ptr<ResponseParameters> parameters;
};

} // namespace TgBot
6 changes: 5 additions & 1 deletion src/Api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,12 @@ nlohmann::json Api::sendRequest(std::string_view method, const std::vector<HttpF
throw TgException(exception.what(), TgException::ErrorCode::InvalidJson);
}
if (!response.value("ok", false)) {
std::shared_ptr<ResponseParameters> parameters;
if (const auto iterator = response.find("parameters"); iterator != response.end()) {
parameters = std::make_shared<ResponseParameters>(iterator->get<ResponseParameters>());
}
throw TgException(response.value("description", "Telegram Bot API request failed"),
static_cast<TgException::ErrorCode>(response.value("error_code", 0)));
static_cast<TgException::ErrorCode>(response.value("error_code", 0)), std::move(parameters));
}

return response.at("result");
Expand Down
8 changes: 6 additions & 2 deletions src/TgException.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
#include "tgbot/TgException.h"

#include <utility>

namespace TgBot {

TgException::TgException(std::string_view description, ErrorCode errorCode)
TgException::TgException(std::string_view description, ErrorCode errorCode,
std::shared_ptr<ResponseParameters> parameters)
: runtime_error(std::string(description))
, errorCode(errorCode) {
, errorCode(errorCode)
, parameters(std::move(parameters)) {
}

} // namespace TgBot
19 changes: 19 additions & 0 deletions tests/tgbot/Api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,25 @@ TEST(Api, GeneratedMethodMapsTelegramErrors) {
EXPECT_TRUE(requestThrows(ErrorCode::InvalidJson, "error_code:101"));
}

TEST(Api, GeneratedMethodPreservesTelegramErrorParameters) {
HttpClientMock httpClient;
httpClient.response
= R"({"ok":false,"error_code":400,"description":"migrated","parameters":{"migrate_to_chat_id":-1001234567890,"retry_after":42}})";
TgBot::Api api("token", httpClient, "url");

try {
api.getMe();
FAIL() << "Expected TgException";
} catch (const TgBot::TgException& exception) {
EXPECT_EQ(exception.errorCode, TgBot::TgException::ErrorCode::BadRequest);
ASSERT_TRUE(exception.parameters);
ASSERT_TRUE(exception.parameters->migrateToChatId);
EXPECT_EQ(*exception.parameters->migrateToChatId, -1001234567890);
ASSERT_TRUE(exception.parameters->retryAfter);
EXPECT_EQ(*exception.parameters->retryAfter, 42);
}
}

TEST(Api, PassesCompleteUrlToHttpClient) {
HttpClientMock httpClient;
httpClient.response = R"({"ok":true,"result":{"id":1,"is_bot":true,"first_name":"bot"}})";
Expand Down
Loading