diff options
-rw-r--r-- | discord/discord.cpp | 13 | ||||
-rw-r--r-- | discord/discord.hpp | 2 | ||||
-rw-r--r-- | discord/http.cpp | 6 |
3 files changed, 20 insertions, 1 deletions
diff --git a/discord/discord.cpp b/discord/discord.cpp index 9cf9df4..02c11ea 100644 --- a/discord/discord.cpp +++ b/discord/discord.cpp @@ -108,6 +108,8 @@ void DiscordClient::UpdateSettingsGuildPositions(const std::vector<Snowflake> &p nlohmann::json body; body["guild_positions"] = pos; m_http.MakePATCH("/users/@me/settings", body.dump(), [this, pos](const cpr::Response &r) { + if (!CheckCode(r)) return; + m_user_settings.GuildPositions = pos; m_abaddon->DiscordNotifyChannelListFullRefresh(); }); @@ -116,6 +118,8 @@ void DiscordClient::UpdateSettingsGuildPositions(const std::vector<Snowflake> &p void DiscordClient::FetchMessagesInChannel(Snowflake id, std::function<void(const std::vector<Snowflake> &)> cb) { std::string path = "/channels/" + std::to_string(id) + "/messages?limit=50"; m_http.MakeGET(path, [this, id, cb](cpr::Response r) { + if (!CheckCode(r)) return; + std::vector<MessageData> msgs; std::vector<Snowflake> ids; @@ -449,6 +453,15 @@ void DiscordClient::SendIdentify() { m_websocket.Send(msg); } +bool DiscordClient::CheckCode(const cpr::Response &r) { + if (r.status_code >= 300) { + fprintf(stderr, "api request to %s failed with status code %d\n", r.url.c_str(), r.status_code); + return false; + } + + return true; +} + void DiscordClient::LoadEventMap() { m_event_map["READY"] = GatewayEvent::READY; m_event_map["MESSAGE_CREATE"] = GatewayEvent::MESSAGE_CREATE; diff --git a/discord/discord.hpp b/discord/discord.hpp index d6577c7..b746778 100644 --- a/discord/discord.hpp +++ b/discord/discord.hpp @@ -94,6 +94,8 @@ private: void HeartbeatThread(); void SendIdentify(); + bool CheckCode(const cpr::Response &r); + Abaddon *m_abaddon = nullptr; HTTPClient m_http; diff --git a/discord/http.cpp b/discord/http.cpp index 454324e..696ab30 100644 --- a/discord/http.cpp +++ b/discord/http.cpp @@ -100,5 +100,9 @@ void HTTPClient::CleanupFutures() { void HTTPClient::OnResponse(cpr::Response r, std::function<void(cpr::Response r)> cb) { CleanupFutures(); - cb(r); + try { + cb(r); + } catch (std::exception &e) { + fprintf(stderr, "error handling response (%s, code %d): %s\n", r.url.c_str(), r.status_code, e.what()); + } } |