Browse Source

Make socket exceptions more specific to match Python semantics

pull/8/head
Joseph Henry 5 years ago
parent
commit
81d03c3692
No known key found for this signature in database
GPG Key ID: C45B33FF5EBC9344
  1. 4
      include/ZeroTierSockets.h
  2. 1
      src/bindings/python/libzt.py
  3. 21
      src/bindings/python/sockets.py

4
include/ZeroTierSockets.h

@ -200,7 +200,7 @@ typedef enum {
extern int zts_errno;
typedef enum {
/** Operation not permitted (`zts_errno` value) */
/** Operation not permitted */
ZTS_EPERM = 1,
/** No such file or directory */
ZTS_ENOENT = 2,
@ -278,6 +278,8 @@ typedef enum {
ZTS_ENOTCONN = 107,
/** Connection timed out */
ZTS_ETIMEDOUT = 110,
/* Connection refused */
ZTS_ECONNREFUSED = 111,
/** No route to host */
ZTS_EHOSTUNREACH = 113,
/** Operation already in progress */

1
src/bindings/python/libzt.py

@ -158,6 +158,7 @@ ZTS_ENOBUFS = _libzt.ZTS_ENOBUFS
ZTS_EISCONN = _libzt.ZTS_EISCONN
ZTS_ENOTCONN = _libzt.ZTS_ENOTCONN
ZTS_ETIMEDOUT = _libzt.ZTS_ETIMEDOUT
ZTS_ECONNREFUSED = _libzt.ZTS_ECONNREFUSED
ZTS_EHOSTUNREACH = _libzt.ZTS_EHOSTUNREACH
ZTS_EALREADY = _libzt.ZTS_EALREADY
ZTS_EINPROGRESS = _libzt.ZTS_EINPROGRESS

21
src/bindings/python/sockets.py

@ -6,6 +6,27 @@ import libzt
def handle_error(err):
"""Convert libzt error code to exception"""
if err == libzt.ZTS_ERR_SOCKET:
if errno() == libzt.ZTS_EAGAIN:
raise BlockingIOError()
return
if errno() == libzt.ZTS_EINPROGRESS:
raise BlockingIOError()
return
if errno() == libzt.ZTS_EALREADY:
raise BlockingIOError()
return
if errno() == libzt.ZTS_ECONNABORTED:
raise ConnectionAbortedError()
return
if errno() == libzt.ZTS_ECONNREFUSED:
raise ConnectionRefusedError()
return
if errno() == libzt.ZTS_ECONNRESET:
raise ConnectionResetError()
return
if errno() == libzt.ZTS_ETIMEDOUT:
raise TimeoutError()
return
raise Exception("ZTS_ERR_SOCKET (" + str(err) + ")")
if err == libzt.ZTS_ERR_SERVICE:
raise Exception("ZTS_ERR_SERVICE (" + str(err) + ")")

Loading…
Cancel
Save