@@ -278,24 +278,19 @@ def send_bcm(bcm_socket: socket.socket, data: bytes) -> int:
278
278
"""
279
279
try :
280
280
return bcm_socket .send (data )
281
- except OSError as e :
282
- base = "Couldn't send CAN BCM frame. OS Error {}: {}\n " .format (
283
- e .errno , e .strerror
284
- )
285
-
286
- if e .errno == errno .EINVAL :
287
- raise can .CanError (
288
- base + "You are probably referring to a non-existing frame."
289
- )
290
-
291
- elif e .errno == errno .ENETDOWN :
292
- raise can .CanError (base + "The CAN interface appears to be down." )
293
-
294
- elif e .errno == errno .EBADF :
295
- raise can .CanError (base + "The CAN socket appears to be closed." )
296
-
281
+ except OSError as error :
282
+ base = f"Couldn't send CAN BCM frame due to OS Error: { error .strerror } "
283
+
284
+ if error .errno == errno .EINVAL :
285
+ specific_message = " You are probably referring to a non-existing frame."
286
+ elif error .errno == errno .ENETDOWN :
287
+ specific_message = " The CAN interface appears to be down."
288
+ elif error .errno == errno .EBADF :
289
+ specific_message = " The CAN socket appears to be closed."
297
290
else :
298
- raise e
291
+ specific_message = ""
292
+
293
+ raise can .CanOperationError (base + specific_message , error .errno ) from error
299
294
300
295
301
296
def _compose_arbitration_id (message : Message ) -> int :
@@ -330,7 +325,7 @@ def __init__(
330
325
messages : Union [Sequence [Message ], Message ],
331
326
period : float ,
332
327
duration : Optional [float ] = None ,
333
- ):
328
+ ) -> None :
334
329
"""Construct and :meth:`~start` a task.
335
330
336
331
:param bcm_socket: An open BCM socket on the desired CAN channel.
@@ -378,7 +373,7 @@ def _tx_setup(self, messages: Sequence[Message]) -> None:
378
373
log .debug ("Sending BCM command" )
379
374
send_bcm (self .bcm_socket , header + body )
380
375
381
- def _check_bcm_task (self ):
376
+ def _check_bcm_task (self ) -> None :
382
377
# Do a TX_READ on a task ID, and check if we get EINVAL. If so,
383
378
# then we are referring to a CAN message with the existing ID
384
379
check_header = build_bcm_header (
@@ -394,14 +389,13 @@ def _check_bcm_task(self):
394
389
)
395
390
try :
396
391
self .bcm_socket .send (check_header )
397
- except OSError as e :
398
- if e .errno != errno .EINVAL :
399
- raise e
392
+ except OSError as error :
393
+ if error .errno != errno .EINVAL :
394
+ raise can . CanOperationError ( "failed to check" , error . errno ) from error
400
395
else :
401
- raise ValueError (
402
- "A periodic task for Task ID {} is already in progress by SocketCAN Linux layer" .format (
403
- self .task_id
404
- )
396
+ raise can .CanOperationError (
397
+ f"A periodic task for task ID { self .task_id } is already in progress "
398
+ "by the SocketCAN Linux layer"
405
399
)
406
400
407
401
def stop (self ) -> None :
@@ -537,7 +531,7 @@ def capture_message(
537
531
else :
538
532
channel = None
539
533
except socket .error as error :
540
- raise can .CanError (f"Error receiving: { error } " )
534
+ raise can .CanOperationError (f"Error receiving: { error . strerror } " , error . errno )
541
535
542
536
can_id , can_dlc , flags , data = dissect_can_frame (cf )
543
537
@@ -550,7 +544,7 @@ def capture_message(
550
544
# see https://man7.org/linux/man-pages/man3/timespec.3.html -> struct timespec for details
551
545
seconds , nanoseconds = RECEIVED_TIMESTAMP_STRUCT .unpack_from (cmsg_data )
552
546
if nanoseconds >= 1e9 :
553
- raise can .CanError (
547
+ raise can .CanOperationError (
554
548
f"Timestamp nanoseconds field was out of range: { nanoseconds } not less than 1e9"
555
549
)
556
550
timestamp = seconds + nanoseconds * 1e-9
@@ -716,9 +710,11 @@ def _recv_internal(
716
710
# get all sockets that are ready (can be a list with a single value
717
711
# being self.socket or an empty list if self.socket is not ready)
718
712
ready_receive_sockets , _ , _ = select .select ([self .socket ], [], [], timeout )
719
- except socket .error as exc :
713
+ except socket .error as error :
720
714
# something bad happened (e.g. the interface went down)
721
- raise can .CanError (f"Failed to receive: { exc } " )
715
+ raise can .CanOperationError (
716
+ f"Failed to receive: { error .strerror } " , error .errno
717
+ )
722
718
723
719
if ready_receive_sockets : # not empty
724
720
get_channel = self .channel == ""
@@ -767,7 +763,7 @@ def send(self, msg: Message, timeout: Optional[float] = None) -> None:
767
763
data = data [sent :]
768
764
time_left = timeout - (time .time () - started )
769
765
770
- raise can .CanError ("Transmit buffer full" )
766
+ raise can .CanOperationError ("Transmit buffer full" )
771
767
772
768
def _send_once (self , data : bytes , channel : Optional [str ] = None ) -> int :
773
769
try :
@@ -776,8 +772,10 @@ def _send_once(self, data: bytes, channel: Optional[str] = None) -> int:
776
772
sent = self .socket .sendto (data , (channel ,))
777
773
else :
778
774
sent = self .socket .send (data )
779
- except socket .error as exc :
780
- raise can .CanError ("Failed to transmit: %s" % exc )
775
+ except socket .error as error :
776
+ raise can .CanOperationError (
777
+ f"Failed to transmit: { error .strerror } " , error .errno
778
+ )
781
779
return sent
782
780
783
781
def _send_periodic_internal (
93C6
table>
0 commit comments