Skip to content
This repository has been archived by the owner on Jan 3, 2025. It is now read-only.

Added error handling #40

Merged
merged 2 commits into from
May 8, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,14 @@ AMeme_API_Controller::~AMeme_API_Controller()
}

// ---------------------------------------------------------------------------------------------------------------------
std::string AMeme_API_Controller::Get_Meme()
std::optional<std::string> AMeme_API_Controller::Get_Meme()
{
if (Meme_Links_Stack.empty())
{
Send_Request("GET");
if (!Send_Request("GET"))
{
return std::nullopt;
}

for (std::size_t i{}; !Json_Response.empty() && i < 12; ++i) // 12 - default count of meme links
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <stack>
#include <vector>
#include <string>
#include <optional>

#include <controller/api/base_api/base_api_controller.hpp>

Expand All @@ -38,7 +39,7 @@ class AMeme_API_Controller : public ABase_API_Controller
explicit AMeme_API_Controller(const AMeme_API_Config& meme_api_config);
~AMeme_API_Controller() override;

std::string Get_Meme();
std::optional<std::string> Get_Meme();

private:
bool Send_Request(std::string_view request_type);
Expand Down
8 changes: 7 additions & 1 deletion source/view/handlers/message_handler/message_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,13 @@ void AMessage_Handler::Bind_Commands()
std::lock_guard<std::mutex> locker(Mutex);
++Metrics.Meme_Request_Count;

TG_Bot.getApi().sendMessage(message->chat->id, Meme_API_Controller.Get_Meme());
if (auto meme{ Meme_API_Controller.Get_Meme() }; meme != std::nullopt)
{
TG_Bot.getApi().sendMessage(message->chat->id, meme.value());
return;
}

TG_Bot.getApi().sendMessage(message->chat->id, AMessage_Reply::Get_Server_Internal_Error());
});
TG_Bot.getEvents().onCommand(SMessage_Commands::Metrics_Range.data(), [this](TgBot::Message::Ptr message) -> void
{
Expand Down
6 changes: 6 additions & 0 deletions source/view/texts/message_reply/message_reply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,10 @@ std::string AMessage_Reply::Get_Synonym_Msg(std::string_view primary_word, std::
std::string AMessage_Reply::Get_Not_Found_Synonym_Msg()
{
return std::string{ "Мені не вдалося знайти синонім до твого слова) " };
}

// ---------------------------------------------------------------------------------------------------------------------
std::string AMessage_Reply::Get_Server_Internal_Error()
{
return std::string{ "Сталася помилка на стороні сервера!" };
}
1 change: 1 addition & 0 deletions source/view/texts/message_reply/message_reply.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,5 @@ class AMessage_Reply
static std::string Get_Not_Found_Antonym_Msg();
static std::string Get_Synonym_Msg(std::string_view primary_word, std::string_view synonym);
static std::string Get_Not_Found_Synonym_Msg();
static std::string Get_Server_Internal_Error();
};
Loading