1
0
Fork 0
mirror of https://github.com/polybar/polybar.git synced 2024-11-18 13:55:11 -05:00

fix(github): Handle curl exceptions and bad status codes (#811)

If any error occurs, a warning is logged and -1 
is displayed as the number of notifications
This commit is contained in:
Kókai Péter 2017-11-07 23:29:44 +01:00 committed by Patrick Ziegler
parent dc0edfb994
commit 3f9f2dc37f
2 changed files with 35 additions and 15 deletions

View file

@ -1,7 +1,7 @@
#pragma once #pragma once
#include "settings.hpp"
#include "modules/meta/timer_module.hpp" #include "modules/meta/timer_module.hpp"
#include "settings.hpp"
#include "utils/http.hpp" #include "utils/http.hpp"
POLYBAR_NS POLYBAR_NS
@ -18,6 +18,9 @@ namespace modules {
bool build(builder* builder, const string& tag) const; bool build(builder* builder, const string& tag) const;
private: private:
void update_label(const int);
int get_number_of_notification();
string request();
static constexpr auto TAG_LABEL = "<label>"; static constexpr auto TAG_LABEL = "<label>";
label_t m_label{}; label_t m_label{};

View file

@ -25,11 +25,7 @@ namespace modules {
if (m_formatter->has(TAG_LABEL)) { if (m_formatter->has(TAG_LABEL)) {
m_label = load_optional_label(m_conf, name(), TAG_LABEL, "Notifications: %notifications%"); m_label = load_optional_label(m_conf, name(), TAG_LABEL, "Notifications: %notifications%");
if (m_empty_notifications) { update_label(0);
m_label->replace_token("%notifications%", "0");
} else {
m_label->clear();
}
} }
} }
@ -37,9 +33,17 @@ namespace modules {
* Update module contents * Update module contents
*/ */
bool github_module::update() { bool github_module::update() {
string content{m_http->get("https://api.github.com/notifications?access_token=" + m_accesstoken)}; auto notification = get_number_of_notification();
long response_code{m_http->response_code()};
update_label(notification);
return true;
}
string github_module::request() {
string content{m_http->get("https://api.github.com/notifications?access_token=" + m_accesstoken)};
long response_code{m_http->response_code()};
switch (response_code) { switch (response_code) {
case 200: case 200:
break; break;
@ -51,6 +55,18 @@ namespace modules {
throw module_error("Unspecified error (" + to_string(response_code) + ")"); throw module_error("Unspecified error (" + to_string(response_code) + ")");
} }
return content;
}
int github_module::get_number_of_notification() {
string content;
try {
content = request();
} catch (application_error& e) {
m_log.warn("%s: cannot complete the request to github: %s", name(), e.what());
return -1;
}
size_t pos{0}; size_t pos{0};
size_t notifications{0}; size_t notifications{0};
@ -58,25 +74,26 @@ namespace modules {
notifications++; notifications++;
} }
if (notifications || m_empty_notifications) { return notifications;
}
void github_module::update_label(const int notifications) {
if (0 != notifications || m_empty_notifications) {
m_label->reset_tokens(); m_label->reset_tokens();
m_label->replace_token("%notifications%", to_string(notifications)); m_label->replace_token("%notifications%", to_string(notifications));
} else { } else {
m_label->clear(); m_label->clear();
} }
return true;
} }
/** /**
* Build module content * Build module content
*/ */
bool github_module::build(builder* builder, const string& tag) const { bool github_module::build(builder* builder, const string& tag) const {
if (tag == TAG_LABEL) { if (tag != TAG_LABEL)
builder->node(m_label);
} else {
return false; return false;
}
builder->node(m_label);
return true; return true;
} }
} }