Skip to content

Commit 549b2a1

Browse files
committed
fix(bar): handle ipc connection errors.
Try to use the default bar id (`bar-0`) if none is set.
1 parent d0f6b41 commit 549b2a1

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

src/bar.cpp

+9-1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ const Bar::bar_mode_map Bar::PRESET_MODES = { //
6161

6262
const std::string_view Bar::MODE_DEFAULT = "default";
6363
const std::string_view Bar::MODE_INVISIBLE = "invisible";
64+
const std::string_view DEFAULT_BAR_ID = "bar-0";
6465

6566
#ifdef HAVE_GTK_LAYER_SHELL
6667
struct GLSSurfaceImpl : public BarSurface, public sigc::trackable {
@@ -556,7 +557,14 @@ waybar::Bar::Bar(struct waybar_output* w_output, const Json::Value& w_config)
556557
if (auto id = config["id"]; id.isString()) {
557558
bar_id = id.asString();
558559
}
559-
_ipc_client = std::make_unique<BarIpcClient>(*this);
560+
if (bar_id.empty()) {
561+
bar_id = DEFAULT_BAR_ID;
562+
}
563+
try {
564+
_ipc_client = std::make_unique<BarIpcClient>(*this);
565+
} catch (const std::exception& exc) {
566+
spdlog::warn("Failed to open bar ipc connection: {}", exc.what());
567+
}
560568
}
561569
#endif
562570

src/modules/sway/bar.cpp

+8-6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#include <fmt/ostream.h>
44
#include <spdlog/spdlog.h>
55

6+
#include <stdexcept>
7+
68
#include "bar.hpp"
79
#include "modules/sway/ipc/ipc.hpp"
810

@@ -47,13 +49,13 @@ struct swaybar_config parseConfig(const Json::Value& payload) {
4749
}
4850

4951
void BarIpcClient::onInitialConfig(const struct Ipc::ipc_response& res) {
50-
try {
51-
auto payload = parser_.parse(res.payload);
52-
auto config = parseConfig(payload);
53-
onConfigUpdate(config);
54-
} catch (const std::exception& e) {
55-
spdlog::error("BarIpcClient::onInitialConfig {}", e.what());
52+
auto payload = parser_.parse(res.payload);
53+
if (auto success = payload.get("success", true); !success.asBool()) {
54+
auto err = payload.get("error", "Unknown error");
55+
throw std::runtime_error(err.asString());
5656
}
57+
auto config = parseConfig(payload);
58+
onConfigUpdate(config);
5759
}
5860

5961
void BarIpcClient::onIpcEvent(const struct Ipc::ipc_response& res) {

0 commit comments

Comments
 (0)