hyprland/backend: throw runtime_error instead of log

Allows us to disable modules entirely when socket connection isn't
working. This is similar to how sway handles their socket connections
disabling modules. This supports a single waybar config for multiple
IPCs.
This commit is contained in:
Austin Horstman 2024-07-16 14:53:54 -05:00
parent 3f61df4e66
commit 4295faa7c4
No known key found for this signature in database
3 changed files with 11 additions and 14 deletions

View File

@ -153,8 +153,7 @@ std::string IPC::getSocket1Reply(const std::string& rq) {
const auto serverSocket = socket(AF_UNIX, SOCK_STREAM, 0); const auto serverSocket = socket(AF_UNIX, SOCK_STREAM, 0);
if (serverSocket < 0) { if (serverSocket < 0) {
spdlog::error("Hyprland IPC: Couldn't open a socket (1)"); throw std::runtime_error("Hyprland IPC: Couldn't open a socket (1)");
return "";
} }
memset(&aiHints, 0, sizeof(struct addrinfo)); memset(&aiHints, 0, sizeof(struct addrinfo));
@ -162,16 +161,15 @@ std::string IPC::getSocket1Reply(const std::string& rq) {
aiHints.ai_socktype = SOCK_STREAM; aiHints.ai_socktype = SOCK_STREAM;
if (getaddrinfo("localhost", nullptr, &aiHints, &aiRes) != 0) { if (getaddrinfo("localhost", nullptr, &aiHints, &aiRes) != 0) {
spdlog::error("Hyprland IPC: Couldn't get host (2)"); throw std::runtime_error("Hyprland IPC: Couldn't get host (2)");
return "";
} }
// get the instance signature // get the instance signature
auto* instanceSig = getenv("HYPRLAND_INSTANCE_SIGNATURE"); auto* instanceSig = getenv("HYPRLAND_INSTANCE_SIGNATURE");
if (instanceSig == nullptr) { if (instanceSig == nullptr) {
spdlog::error("Hyprland IPC: HYPRLAND_INSTANCE_SIGNATURE was not set! (Is Hyprland running?)"); throw std::runtime_error(
return ""; "Hyprland IPC: HYPRLAND_INSTANCE_SIGNATURE was not set! (Is Hyprland running?)");
} }
sockaddr_un serverAddress = {0}; sockaddr_un serverAddress = {0};
@ -182,14 +180,12 @@ std::string IPC::getSocket1Reply(const std::string& rq) {
// Use snprintf to copy the socketPath string into serverAddress.sun_path // Use snprintf to copy the socketPath string into serverAddress.sun_path
if (snprintf(serverAddress.sun_path, sizeof(serverAddress.sun_path), "%s", socketPath.c_str()) < if (snprintf(serverAddress.sun_path, sizeof(serverAddress.sun_path), "%s", socketPath.c_str()) <
0) { 0) {
spdlog::error("Hyprland IPC: Couldn't copy socket path (6)"); throw std::runtime_error("Hyprland IPC: Couldn't copy socket path (6)");
return "";
} }
if (connect(serverSocket, reinterpret_cast<sockaddr*>(&serverAddress), sizeof(serverAddress)) < if (connect(serverSocket, reinterpret_cast<sockaddr*>(&serverAddress), sizeof(serverAddress)) <
0) { 0) {
spdlog::error("Hyprland IPC: Couldn't connect to " + socketPath + ". (3)"); throw std::runtime_error("Hyprland IPC: Couldn't connect to " + socketPath + ". (3)");
return "";
} }
auto sizeWritten = write(serverSocket, rq.c_str(), rq.length()); auto sizeWritten = write(serverSocket, rq.c_str(), rq.length());

View File

@ -52,10 +52,8 @@ TEST_CASE_METHOD(IPCTestFixture, "XDGRuntimeDirExistsNoHyprDir", "[getSocketFold
REQUIRE(actualPath == expectedPath); REQUIRE(actualPath == expectedPath);
} }
TEST_CASE_METHOD(IPCMock, "getSocket1JsonReply handles empty response", "[getSocket1JsonReply]") { TEST_CASE_METHOD(IPCTestFixture, "getSocket1Reply throws on no socket", "[getSocket1Reply]") {
std::string request = "test_request"; std::string request = "test_request";
Json::Value jsonResponse = getSocket1JsonReply(request); CHECK_THROWS(getSocket1Reply(request));
REQUIRE(jsonResponse.isNull());
} }

View File

@ -19,4 +19,7 @@ class IPCMock : public IPCTestFixture {
public: public:
// Mock getSocket1Reply to return an empty string // Mock getSocket1Reply to return an empty string
static std::string getSocket1Reply(const std::string& rq) { return ""; } static std::string getSocket1Reply(const std::string& rq) { return ""; }
protected:
const char* instanceSig = "instance_sig";
}; };