Skip to content

Commit 16a1af6

Browse files
committed
Create MeshInterface.MeshInterfaceError to specialize errors in that file
1 parent b864066 commit 16a1af6

1 file changed

Lines changed: 15 additions & 9 deletions

File tree

meshtastic/mesh_interface.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ class MeshInterface:
4747
debugOut
4848
"""
4949

50+
class MeshInterfaceError(Exception):
51+
"""An exception class for general mesh interface errors"""
52+
def __init__(self, message):
53+
self.message = message
54+
super().__init__(self.message)
55+
5056
def __init__(self, debugOut=None, noProto=False):
5157
"""Constructor
5258
@@ -313,7 +319,7 @@ def sendData(
313319
f"mesh_pb2.Constants.DATA_PAYLOAD_LEN: {mesh_pb2.Constants.DATA_PAYLOAD_LEN}"
314320
)
315321
if len(data) > mesh_pb2.Constants.DATA_PAYLOAD_LEN:
316-
raise Exception("Data payload too big")
322+
raise MeshInterface.MeshInterfaceError("Data payload too big")
317323

318324
if (
319325
portNum == portnums_pb2.PortNum.UNKNOWN_APP
@@ -542,25 +548,25 @@ def waitForConfig(self):
542548
and self.localNode.waitForConfig()
543549
)
544550
if not success:
545-
raise Exception("Timed out waiting for interface config")
551+
raise MeshInterface.MeshInterfaceError("Timed out waiting for interface config")
546552

547553
def waitForAckNak(self):
548554
"""Wait for the ack/nak"""
549555
success = self._timeout.waitForAckNak(self._acknowledgment)
550556
if not success:
551-
raise Exception("Timed out waiting for an acknowledgment")
557+
raise MeshInterface.MeshInterfaceError("Timed out waiting for an acknowledgment")
552558

553559
def waitForTraceRoute(self, waitFactor):
554560
"""Wait for trace route"""
555561
success = self._timeout.waitForTraceRoute(waitFactor, self._acknowledgment)
556562
if not success:
557-
raise Exception("Timed out waiting for traceroute")
563+
raise MeshInterface.MeshInterfaceError("Timed out waiting for traceroute")
558564

559565
def waitForTelemetry(self):
560566
"""Wait for telemetry"""
561567
success = self._timeout.waitForTelemetry(self._acknowledgment)
562568
if not success:
563-
raise Exception("Timed out waiting for telemetry")
569+
raise MeshInterface.MeshInterfaceError("Timed out waiting for telemetry")
564570

565571
def getMyNodeInfo(self):
566572
"""Get info about my node."""
@@ -595,7 +601,7 @@ def _waitConnected(self, timeout=30.0):
595601
and raise an exception"""
596602
if not self.noProto:
597603
if not self.isConnected.wait(timeout): # timeout after x seconds
598-
raise Exception("Timed out waiting for connection completion")
604+
raise MeshInterface.MeshInterfaceError("Timed out waiting for connection completion")
599605

600606
# If we failed while connecting, raise the connection to the client
601607
if self.failure:
@@ -604,7 +610,7 @@ def _waitConnected(self, timeout=30.0):
604610
def _generatePacketId(self):
605611
"""Get a new unique packet ID"""
606612
if self.currentPacketId is None:
607-
raise Exception("Not connected yet, can not generate packet")
613+
raise MeshInterface.MeshInterfaceError("Not connected yet, can not generate packet")
608614
else:
609615
self.currentPacketId = (self.currentPacketId + 1) & 0xFFFFFFFF
610616
return self.currentPacketId
@@ -773,7 +779,7 @@ def _handleFromRadio(self, fromRadioBytes):
773779
failmsg = None
774780

775781
if failmsg:
776-
self.failure = Exception(failmsg)
782+
self.failure = MeshInterface.MeshInterfaceError(failmsg)
777783
self.isConnected.set() # let waitConnected return this exception
778784
self.close()
779785

@@ -919,7 +925,7 @@ def _nodeNumToId(self, num):
919925
def _getOrCreateByNum(self, nodeNum):
920926
"""Given a nodenum find the NodeInfo in the DB (or create if necessary)"""
921927
if nodeNum == BROADCAST_NUM:
922-
raise Exception("Can not create/find nodenum by the broadcast num")
928+
raise MeshInterface.MeshInterfaceError("Can not create/find nodenum by the broadcast num")
923929

924930
if nodeNum in self.nodesByNum:
925931
return self.nodesByNum[nodeNum]

0 commit comments

Comments
 (0)