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_;
int fd_event_; int fd_event_;
std::mutex mutex_; std::mutex mutex_;
std::mutex mutex_event_;
}; };
} // namespace waybar::modules::sway } // 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")) { if (module.isMember("format-alt-click")) {
Json::Value& click = module["format-alt-click"]; Json::Value& click = module["format-alt-click"];
if (click.isString()) { if (click.isString()) {
std::string str_click = click.asString(); if (click == "click-right") {
if (str_click == "click-right") {
module["format-alt-click"] = 3U; module["format-alt-click"] = 3U;
} else if (str_click == "click-middle") { } else if (click == "click-middle") {
module["format-alt-click"] = 2U; module["format-alt-click"] = 2U;
} else if (str_click == "click-backward") { } else if (click == "click-backward") {
module["format-alt-click"] = 8U; module["format-alt-click"] = 8U;
} else if (str_click == "click-forward") { } else if (click == "click-forward") {
module["format-alt-click"] = 9U; module["format-alt-click"] = 9U;
} else { } else {
module["format-alt-click"] = 1U; // default click-left 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) { void Ipc::subscribe(const std::string& payload) {
std::lock_guard<std::mutex> lock(mutex_event_);
auto res = Ipc::send(fd_event_, IPC_SUBSCRIBE, payload); auto res = Ipc::send(fd_event_, IPC_SUBSCRIBE, payload);
if (res.payload != "{\"success\": true}") { if (res.payload != "{\"success\": true}") {
throw std::runtime_error("Unable to subscribe ipc event"); throw std::runtime_error("Unable to subscribe ipc event");
@ -139,7 +138,6 @@ void Ipc::subscribe(const std::string& payload) {
} }
void Ipc::handleEvent() { void Ipc::handleEvent() {
std::lock_guard<std::mutex> lock(mutex_event_);
const auto res = Ipc::recv(fd_event_); const auto res = Ipc::recv(fd_event_);
signal_event.emit(res); signal_event.emit(res);
} }

View File

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

View File

@ -24,6 +24,7 @@ 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::onEvent(const struct Ipc::ipc_response& res) { getTree(); }
void Window::onCmd(const struct Ipc::ipc_response& res) { void Window::onCmd(const struct Ipc::ipc_response& res) {
try {
auto payload = parser_.parse(res.payload); auto payload = parser_.parse(res.payload);
auto [nb, id, name, app_id] = getFocusedNode(payload); auto [nb, id, name, app_id] = getFocusedNode(payload);
if (!app_id_.empty()) { if (!app_id_.empty()) {
@ -46,6 +47,9 @@ void Window::onCmd(const struct Ipc::ipc_response& res) {
window_ = name; window_ = name;
dp.emit(); dp.emit();
} }
} catch (const std::exception& e) {
std::cerr << "Window: " << e.what() << std::endl;
}
} }
void Window::worker() { void Window::worker() {

View File

@ -23,6 +23,7 @@ void Workspaces::onEvent(const struct Ipc::ipc_response &res) { ipc_.sendCmd(IPC
void Workspaces::onCmd(const struct Ipc::ipc_response &res) { void Workspaces::onCmd(const struct Ipc::ipc_response &res) {
if (res.type == IPC_GET_WORKSPACES) { if (res.type == IPC_GET_WORKSPACES) {
try {
auto payload = parser_.parse(res.payload); auto payload = parser_.parse(res.payload);
if (payload.isArray()) { if (payload.isArray()) {
std::lock_guard<std::mutex> lock(mutex_); std::lock_guard<std::mutex> lock(mutex_);
@ -37,6 +38,9 @@ void Workspaces::onCmd(const struct Ipc::ipc_response &res) {
}); });
dp.emit(); dp.emit();
} }
} catch (const std::exception &e) {
std::cerr << "Workspaces: " << e.what() << std::endl;
}
} else { } else {
if (scrolling_) { if (scrolling_) {
scrolling_ = false; scrolling_ = false;