Python Client  0.0.0.0
Python wrapper for Corelink DLL
corelink_exception.py
Go to the documentation of this file.
1 """
2  @file corelink_exception.py
3  @brief Handles the underlying DLL error messages
4 """
5 import ctypes
6 import corelink_const
7 
8 class CorelinkException(Exception):
9  """
10  Gets the error message for the corresponding error ID.
11  @param error_id id returned by call to the dll.
12  @exception Throws a CorelinkException for the error_id if error_id != 0.
13  """
14  def __init__(self,*arg):
15  if len(arg)==2:
16  self.update(arg[0],arg[1])
17  if len(arg)==1:
18  rhs = arg[0]
19  self.update(rhs.msg,rhs.code)
20  super().__init__(self.error_msg)
21  def update(self,msg,code):
22  """
23  Update the Parameters
24  """
25  self.msg = msg
26  self.code = code
27  self.error_msg = corelink_const.ERROR_CODE_STRING[code] + ": " + msg
28  @staticmethod
29  def get_dll_exception(error_id_ctypes):
30  """
31  Handle DLL Exception
32  """
33  error_id = error_id_ctypes.value
34  if error_id==0:
35  return
36  error_len = ctypes.c_int()
37  corelink_const.LIB.getError.argtypes = [ctypes.POINTER(ctypes.c_int),
38  ctypes.POINTER(ctypes.c_int)]
39  corelink_const.LIB.getError.restype = ctypes.c_char_p
40  msg = corelink_const.LIB.getError(ctypes.byref(error_id_ctypes),
41  ctypes.byref(error_len)).decode('UTF-8')
42  if error_len.value ==0:
43  raise CorelinkException("No message remaining. Client closed",
44  corelink_const.ERROR_CODES['ERROR_CODE_NONE'])
45  raise CorelinkException(msg[:error_len.value -1],ord(msg[error_len.value -1]))