refactor: try/catch around json parse

This commit is contained in:
Alex 2019-05-09 10:30:54 +02:00
parent fd9b34adf8
commit 5bf0ca85ac
6 changed files with 55 additions and 48 deletions

View File

@ -42,7 +42,6 @@ class Ipc {
int fd_;
int fd_event_;
std::mutex mutex_;
std::mutex mutex_event_;
};
} // namespace waybar::modules::sway

View File

@ -111,15 +111,13 @@ void waybar::Bar::setupAltFormatKeyForModule(const std::string& module_name) {
if (module.isMember("format-alt-click")) {
Json::Value& click = module["format-alt-click"];
if (click.isString()) {
std::string str_click = click.asString();
if (str_click == "click-right") {
if (click == "click-right") {
module["format-alt-click"] = 3U;
} else if (str_click == "click-middle") {
} else if (click == "click-middle") {
module["format-alt-click"] = 2U;
} else if (str_click == "click-backward") {
} else if (click == "click-backward") {
module["format-alt-click"] = 8U;
} else if (str_click == "click-forward") {
} else if (click == "click-forward") {
module["format-alt-click"] = 9U;
} else {
module["format-alt-click"] = 1U; // default click-left

View File

@ -131,7 +131,6 @@ void Ipc::sendCmd(uint32_t type, const std::string& payload) {
}
void Ipc::subscribe(const std::string& payload) {
std::lock_guard<std::mutex> lock(mutex_event_);
auto res = Ipc::send(fd_event_, IPC_SUBSCRIBE, payload);
if (res.payload != "{\"success\": true}") {
throw std::runtime_error("Unable to subscribe ipc event");
@ -139,7 +138,6 @@ void Ipc::subscribe(const std::string& payload) {
}
void Ipc::handleEvent() {
std::lock_guard<std::mutex> lock(mutex_event_);
const auto res = Ipc::recv(fd_event_);
signal_event.emit(res);
}

View File

@ -15,13 +15,17 @@ Mode::Mode(const std::string& id, const Json::Value& config) : ALabel(config, "{
}
void Mode::onEvent(const struct Ipc::ipc_response& res) {
auto payload = parser_.parse(res.payload);
if (payload["change"] != "default") {
mode_ = payload["change"].asString();
} else {
mode_.clear();
try {
auto payload = parser_.parse(res.payload);
if (payload["change"] != "default") {
mode_ = payload["change"].asString();
} else {
mode_.clear();
}
dp.emit();
} catch (const std::exception& e) {
std::cerr << "Mode: " << e.what() << std::endl;
}
dp.emit();
}
void Mode::worker() {

View File

@ -24,27 +24,31 @@ Window::Window(const std::string& id, const Bar& bar, const Json::Value& config)
void Window::onEvent(const struct Ipc::ipc_response& res) { getTree(); }
void Window::onCmd(const struct Ipc::ipc_response& res) {
auto payload = parser_.parse(res.payload);
auto [nb, id, name, app_id] = getFocusedNode(payload);
if (!app_id_.empty()) {
bar_.window.get_style_context()->remove_class(app_id_);
}
if (nb == 0) {
bar_.window.get_style_context()->add_class("empty");
} else if (nb == 1) {
bar_.window.get_style_context()->add_class("solo");
if (!app_id.empty()) {
bar_.window.get_style_context()->add_class(app_id);
try {
auto payload = parser_.parse(res.payload);
auto [nb, id, name, app_id] = getFocusedNode(payload);
if (!app_id_.empty()) {
bar_.window.get_style_context()->remove_class(app_id_);
}
} else {
bar_.window.get_style_context()->remove_class("solo");
bar_.window.get_style_context()->remove_class("empty");
}
app_id_ = app_id;
if (windowId_ != id || window_ != name) {
windowId_ = id;
window_ = name;
dp.emit();
if (nb == 0) {
bar_.window.get_style_context()->add_class("empty");
} else if (nb == 1) {
bar_.window.get_style_context()->add_class("solo");
if (!app_id.empty()) {
bar_.window.get_style_context()->add_class(app_id);
}
} else {
bar_.window.get_style_context()->remove_class("solo");
bar_.window.get_style_context()->remove_class("empty");
}
app_id_ = app_id;
if (windowId_ != id || window_ != name) {
windowId_ = id;
window_ = name;
dp.emit();
}
} catch (const std::exception& e) {
std::cerr << "Window: " << e.what() << std::endl;
}
}

View File

@ -23,19 +23,23 @@ void Workspaces::onEvent(const struct Ipc::ipc_response &res) { ipc_.sendCmd(IPC
void Workspaces::onCmd(const struct Ipc::ipc_response &res) {
if (res.type == IPC_GET_WORKSPACES) {
auto payload = parser_.parse(res.payload);
if (payload.isArray()) {
std::lock_guard<std::mutex> lock(mutex_);
workspaces_.clear();
std::copy_if(payload.begin(),
payload.end(),
std::back_inserter(workspaces_),
[&](const auto &workspace) {
return !config_["all-outputs"].asBool()
? workspace["output"].asString() == bar_.output->name
: true;
});
dp.emit();
try {
auto payload = parser_.parse(res.payload);
if (payload.isArray()) {
std::lock_guard<std::mutex> lock(mutex_);
workspaces_.clear();
std::copy_if(payload.begin(),
payload.end(),
std::back_inserter(workspaces_),
[&](const auto &workspace) {
return !config_["all-outputs"].asBool()
? workspace["output"].asString() == bar_.output->name
: true;
});
dp.emit();
}
} catch (const std::exception &e) {
std::cerr << "Workspaces: " << e.what() << std::endl;
}
} else {
if (scrolling_) {