handle http errors a bit better

This commit is contained in:
ouwou
2020-09-05 20:59:17 -04:00
parent 5e0a5bb964
commit 8e749ac3bf
3 changed files with 20 additions and 1 deletions

View File

@@ -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;

View File

@@ -94,6 +94,8 @@ private:
void HeartbeatThread();
void SendIdentify();
bool CheckCode(const cpr::Response &r);
Abaddon *m_abaddon = nullptr;
HTTPClient m_http;

View File

@@ -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());
}
}