From 69a2bf211ad431692838533559071e2c8af1624f Mon Sep 17 00:00:00 2001 From: patrick96 Date: Sat, 29 Jan 2022 17:44:54 +0100 Subject: [PATCH] polybar-msg: Better error messages If a single connection fails, other connections are kept intact. --- src/polybar-msg.cpp | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/polybar-msg.cpp b/src/polybar-msg.cpp index 4be0bef7..44dd22b9 100644 --- a/src/polybar-msg.cpp +++ b/src/polybar-msg.cpp @@ -33,8 +33,8 @@ void error(const string& msg) { throw std::runtime_error(msg); } -void uv_error(int status, const string& msg) { - throw std::runtime_error(msg + " (" + uv_strerror(status) + ")"); +void uv_error(int status, int pid, const string& msg) { + fprintf(stderr, "%s: %s (PID: %d)\n", msg.c_str(), uv_strerror(status), pid); } void usage(FILE* f, const string& parameters) { @@ -76,7 +76,7 @@ static vector get_sockets() { return sockets; } -static void on_write(PipeHandle& conn, ipc::decoder& dec) { +static void on_write(PipeHandle& conn, ipc::decoder& dec, int pid) { conn.read_start( [&](const auto& e) { try { @@ -88,19 +88,19 @@ static void on_write(PipeHandle& conn, ipc::decoder& dec) { } }, [&]() { conn.close(); }, - [&](const auto& e) { + [&, pid](const auto& e) { conn.close(); - uv_error(e.status, "There was an error while reading polybar's response"); + uv_error(e.status, pid, "There was an error while reading polybar's response"); }); } -static void on_connection(PipeHandle& conn, ipc::decoder& dec, const ipc::type_t type, const string& payload) { +static void on_connection(PipeHandle& conn, ipc::decoder& dec, int pid, const ipc::type_t type, const string& payload) { const auto data = ipc::encode(type, payload); conn.write( - data, [&]() { on_write(conn, dec); }, - [&](const auto& e) { + data, [&, pid]() { on_write(conn, dec, pid); }, + [&, pid](const auto& e) { conn.close(); - uv_error(e.status, "There was an error while sending the IPC message."); + uv_error(e.status, pid, "There was an error while sending the IPC message."); }); } @@ -229,7 +229,7 @@ int run(int argc, char** argv) { logger null_logger{loglevel::NONE}; /* - * Store all decoreds in vector so that they're alive for the whole eventloop. + * Store all decoders in vector so that they're alive for the whole eventloop. */ vector decoders; @@ -265,7 +265,9 @@ int run(int argc, char** argv) { auto& conn = loop.handle(); conn.connect( channel, - [&conn, &decoders, type, payload, channel, idx]() { on_connection(conn, decoders[idx], type, payload); }, + [&conn, &decoders, pid, type, payload, channel, idx]() { + on_connection(conn, decoders[idx], pid, type, payload); + }, [&](const auto& e) { fprintf(stderr, "%s: Failed to connect to '%s' (err: '%s')\n", exec, channel.c_str(), uv_strerror(e.status)); success = false;