From c27be716ecd1fa0322cc70654b3dcaa672d8969a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kacper=20R=C4=85czy?= Date: Sat, 3 Jun 2023 00:14:08 +0200 Subject: [PATCH] connect: add error messages when Socket connect fails (#456) * Throw runtime_error in Socket::create * Add messages to MessagingError * Fix cython bindings * Write a message to stderr instead of throwing runtime_error --- messaging/messaging.cc | 8 ++++++-- messaging/messaging_pyx.pyx | 14 +++++++++----- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/messaging/messaging.cc b/messaging/messaging.cc index 511ae38..6b7fe8f 100644 --- a/messaging/messaging.cc +++ b/messaging/messaging.cc @@ -64,8 +64,10 @@ SubSocket * SubSocket::create(Context * context, std::string endpoint, std::stri if (r == 0) { return s; } else { + std::cerr << "Error, failed to connect SubSocket to " << endpoint << ": " << strerror(errno) << std::endl; + delete s; - return NULL; + return nullptr; } } @@ -87,8 +89,10 @@ PubSocket * PubSocket::create(Context * context, std::string endpoint, bool chec if (r == 0) { return s; } else { + std::cerr << "Error, failed to bind PubSocket to " << endpoint << ": " << strerror(errno) << std::endl; + delete s; - return NULL; + return nullptr; } } diff --git a/messaging/messaging_pyx.pyx b/messaging/messaging_pyx.pyx index 618136a..3e07685 100644 --- a/messaging/messaging_pyx.pyx +++ b/messaging/messaging_pyx.pyx @@ -6,6 +6,7 @@ from libcpp.string cimport string from libcpp.vector cimport vector from libcpp cimport bool from libc cimport errno +from libc.string cimport strerror from cython.operator import dereference @@ -18,7 +19,10 @@ from .messaging cimport Event as cppEvent, SocketEventHandle as cppSocketEventHa class MessagingError(Exception): - pass + def __init__(self, endpoint=None): + suffix = "with {endpoint}" if endpoint else "" + message = f"Messaging failure {suffix}: {strerror(errno.errno)}" + super().__init__(message) class MultiplePublishersError(MessagingError): @@ -184,9 +188,9 @@ cdef class SubSocket: if r != 0: if errno.errno == errno.EADDRINUSE: - raise MultiplePublishersError + raise MultiplePublishersError(endpoint) else: - raise MessagingError + raise MessagingError(endpoint) def setTimeout(self, int timeout): self.socket.setTimeout(timeout) @@ -225,9 +229,9 @@ cdef class PubSocket: if r != 0: if errno.errno == errno.EADDRINUSE: - raise MultiplePublishersError + raise MultiplePublishersError(endpoint) else: - raise MessagingError + raise MessagingError(endpoint) def send(self, bytes data): length = len(data)