Skip to content

Commit f507a87

Browse files
committed
wrap the last exception when reporting connect failure
fixes adafruit#113
1 parent ec8b60b commit f507a87

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

adafruit_minimqtt/adafruit_minimqtt.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@ def _get_connect_socket(self, host, port, *, timeout=1):
258258

259259
sock = None
260260
retry_count = 0
261+
last_exception = None
261262
while retry_count < 5 and sock is None:
262263
retry_count += 1
263264

@@ -274,15 +275,20 @@ def _get_connect_socket(self, host, port, *, timeout=1):
274275

275276
try:
276277
sock.connect((connect_host, port))
277-
except MemoryError:
278+
except MemoryError as exc:
278279
sock.close()
279280
sock = None
280-
except OSError:
281+
last_exception = exc
282+
except OSError as exc:
281283
sock.close()
282284
sock = None
285+
last_exception = exc
283286

284287
if sock is None:
285-
raise RuntimeError("Repeated socket failures")
288+
if last_exception:
289+
raise RuntimeError("Repeated socket failures") from last_exception
290+
else:
291+
raise RuntimeError("Repeated socket failures")
286292

287293
self._backwards_compatible_sock = not hasattr(sock, "recv_into")
288294
return sock

0 commit comments

Comments
 (0)