mirror of
https://github.com/celisej567/abaddon.git
synced 2026-09-06 20:15:19 +03:00
handle http errors a bit better
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -94,6 +94,8 @@ private:
|
||||
void HeartbeatThread();
|
||||
void SendIdentify();
|
||||
|
||||
bool CheckCode(const cpr::Response &r);
|
||||
|
||||
Abaddon *m_abaddon = nullptr;
|
||||
HTTPClient m_http;
|
||||
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user