Better error message on socket (#1477)
Build and Deploy on local RHEL9 / build (push) Successful in 2m1s
Build on RHEL9 docker image / build (push) Successful in 4m31s
Build and Deploy on local RHEL8 / build (push) Successful in 5m6s
Build on RHEL8 docker image / build (push) Successful in 5m34s
Run Simulator Tests on local RHEL9 / build (push) Successful in 18m38s
Run Simulator Tests on local RHEL8 / build (push) Successful in 22m6s

* checking errno

* improved socket error message

* improved message

* decode error code

* format

* clearer msg

* report write errors, handle SIGPIPE

* retry and cleaned up timeout

* removed read/write in DataSocket
This commit is contained in:
Erik Fröjdh
2026-06-30 15:29:58 +02:00
committed by GitHub
parent 4395e7b5f6
commit ffec82076d
9 changed files with 475 additions and 62 deletions
+26 -25
View File
@@ -5,9 +5,11 @@
#include "sls/sls_detector_defs.h"
#include "sls/sls_detector_exceptions.h"
#include "sls/sls_detector_funcs.h"
#include "sls/string_utils.h"
#include <arpa/inet.h>
#include <cassert>
#include <cstring>
#include <fmt/format.h>
#include <iostream>
#include <stdexcept>
#include <unistd.h>
@@ -24,9 +26,10 @@ ClientSocket::ClientSocket(std::string stype, const std::string &host,
hints.ai_flags |= AI_CANONNAME;
if (getaddrinfo(host.c_str(), nullptr, &hints, &result) != 0) {
std::string msg = "ClientSocket cannot decode host:" + host +
" on port " + std::to_string(port) + "\n";
throw SocketError(msg);
auto msg = fmt::format("Cannot resolve {} hostname: '{}'", to_lower(socketType), host);
throwError(msg);
}
// TODO! Erik, results could have multiple entries do we need to loop
@@ -40,10 +43,11 @@ ClientSocket::ClientSocket(std::string stype, const std::string &host,
if (::connect(getSocketId(), (struct sockaddr *)&serverAddr,
sizeof(serverAddr)) != 0) {
freeaddrinfo(result);
std::string msg = "ClientSocket: Cannot connect to " + socketType +
":" + host + " on port " + std::to_string(port) +
"\n";
throw SocketError(msg);
auto msg = fmt::format(
"Cannot connect to {} on {}:{}\n",
to_lower(socketType), host, port);
throwError(msg);
}
freeaddrinfo(result);
}
@@ -54,10 +58,18 @@ ClientSocket::ClientSocket(std::string sType, struct sockaddr_in addr)
if (::connect(getSocketId(), (struct sockaddr *)&addr, sizeof(addr)) != 0) {
char address[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &addr.sin_addr, address, INET_ADDRSTRLEN);
std::string msg = "ClientSocket: Cannot connect to " + socketType +
":" + address + " on port " +
std::to_string(addr.sin_port) + "\n";
throw SocketError(msg);
auto msg = fmt::format("Cannot connect to {} on {}:{}", to_lower(socketType), address, addr.sin_port);
throwError(msg);
}
}
void ClientSocket::throwError(const std::string &msg) const {
if (socketType == "Receiver") {
throw ReceiverError(msg);
} else if (socketType == "Detector") {
throw DetectorError(msg);
} else {
throw GuiError(msg);
}
}
@@ -80,26 +92,15 @@ void ClientSocket::readReply(int &ret, void *retval, size_t retval_size) {
std::string mess = readErrorMessage();
// Do we need to know hostname here?
// In that case save it???
if (socketType == "Receiver") {
throw ReceiverError("Receiver returned: " + std::string(mess));
} else if (socketType == "Detector") {
throw DetectorError("Detector returned: " + std::string(mess));
} else {
throw GuiError(mess);
}
throwError(socketType + " returned: " + mess);
}
// get retval
Receive(retval, retval_size);
}
// debugging
catch (SocketError &e) {
if (socketType == "Receiver") {
throw ReceiverError("Receiver returned: " + std::string(e.what()));
} else if (socketType == "Detector") {
throw DetectorError("Detector returned: " + std::string(e.what()));
} else {
throw GuiError(e.what());
}
auto msg = fmt::format("While reading reply from {} {}", to_lower(socketType), e.what());
throwError(msg);
}
}