Skip to content

Commit 4a4a57a

Browse files
committed
Specific Exceptions: Adapting nixnet interface
Part of #1046. Since the NI-XNET interface was not released in a stable release yet (see #968), we can freely change the exception handling without deprecation notices.
1 parent beb5727 commit 4a4a57a

File tree

1 file changed

+19
-33
lines changed

1 file changed

+19
-33
lines changed

can/interfaces/nixnet.py

+19-33
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
import time
1414
import struct
1515

16-
from can import CanError, BusABC, Message
16+
from can import BusABC, Message
17+
from ..exceptions import CanInitializationError, CanOperationError
18+
1719

1820
logger = logging.getLogger(__name__)
1921

@@ -28,18 +30,15 @@
2830
database,
2931
XnetError,
3032
)
31-
except ImportError:
32-
logger.error("Error, NIXNET python module cannot be loaded.")
33-
raise ImportError()
33+
except ImportError as error:
34+
raise ImportError("Error, NIXNET python module cannot be loaded") from error
3435
else:
35-
logger.error("NI-XNET interface is only available on Windows systems")
36-
raise NotImplementedError("NiXNET is not supported on not Win32 platforms")
36+
raise NotImplementedError("NiXNET only supported on Win32 platforms")
3737

3838

3939
class NiXNETcanBus(BusABC):
4040
"""
4141
The CAN Bus implemented for the NI-XNET interface.
42-
4342
"""
4443

4544
def __init__(
@@ -52,7 +51,7 @@ def __init__(
5251
brs=False,
5352
can_termination=False,
5453
log_errors=True,
55-
**kwargs
54+
**kwargs,
5655
):
5756
"""
5857
:param str channel:
@@ -69,9 +68,8 @@ def __init__(
6968
``is_error_frame`` set to True and ``arbitration_id`` will identify
7069
the error (default True)
7170
72-
:raises can.interfaces.nixnet.NiXNETError:
71+
:raises can.exceptions.CanInitializationError:
7372
If starting communication fails
74-
7573
"""
7674
self._rx_queue = []
7775
self.channel = channel
@@ -120,16 +118,18 @@ def __init__(
120118
self.__session_send.start()
121119
self.__session_receive.start()
122120

123-
except errors.XnetError as err:
124-
raise NiXNETError(function="__init__", error_message=err.args[0]) from None
121+
except errors.XnetError as error:
122+
raise CanInitializationError(
123+
f"{error.args[0]} ({error.error_type})", error.error_code
124+
) from None
125125

126126
self._is_filtered = False
127127
super(NiXNETcanBus, self).__init__(
128128
channel=channel,
129129
can_filters=can_filters,
130130
bitrate=bitrate,
131131
log_errors=log_errors,
132-
**kwargs
132+
**kwargs,
133133
)
134134

135135
def _recv_internal(self, timeout):
@@ -160,8 +160,7 @@ def _recv_internal(self, timeout):
160160
)
161161

162162
return msg, self._filters is None
163-
except Exception as e:
164-
# print('Error: ', e)
163+
except Exception:
165164
return None, self._filters is None
166165

167166
def send(self, msg, timeout=None):
@@ -174,7 +173,7 @@ def send(self, msg, timeout=None):
174173
:param float timeout:
175174
Max time to wait for the device to be ready in seconds, None if time is infinite
176175
177-
:raises can.interfaces.nixnet.NiXNETError:
176+
:raises can.exceptions.CanOperationError:
178177
If writing to transmit buffer fails.
179178
It does not wait for message to be ACKed currently.
180179
"""
@@ -201,8 +200,10 @@ def send(self, msg, timeout=None):
201200

202201
try:
203202
self.__session_send.frames.write([can_frame], timeout)
204-
except errors.XnetError as err:
205-
raise NiXNETError(function="send", error_message=err.args[0]) from None
203+
except errors.XnetError as error:
204+
raise CanOperationError(
205+
f"{error.args[0]} ({error.error_type})", error.error_code
206+
) from None
206207

207208
def reset(self):
208209
"""
@@ -253,18 +254,3 @@ def _detect_available_configs():
253254
logger.debug("An error occured while searching for configs: %s", str(error))
254255

255256
return configs
256-
257-
258-
# To-Do review error management, I don't like this implementation
259-
class NiXNETError(CanError):
260-
"""Error from NI-XNET driver."""
261-
262-
def __init__(self, function="", error_message=""):
263-
super(NiXNETError, self).__init__()
264-
#: Function that failed
265-
self.function = function
266-
#: Arguments passed to function
267-
self.error_message = error_message
268-
269-
def __str__(self):
270-
return "Function %s failed:\n%s" % (self.function, self.error_message)

0 commit comments

Comments
 (0)