extends error class

now accepts a custom message

fn_name and error code stored as class attributes
This commit is contained in:
onyx-and-iris 2023-07-10 15:36:38 +01:00
parent af368b4b0a
commit bafaa58507
2 changed files with 14 additions and 5 deletions

View File

@ -16,7 +16,7 @@ class CBindings(metaclass=ABCMeta):
Maps expected ctype argument and res types for each binding.
"""
logger_cbindings = logger.getChild("Cbindings")
logger_cbindings = logger.getChild("CBindings")
vm_login = libc.VBVMR_Login
vm_login.restype = LONG
@ -116,10 +116,10 @@ class CBindings(metaclass=ABCMeta):
res = func(*args)
if ok_exp is None:
if res not in ok:
raise CAPIError(f"{func.__name__} returned {res}")
elif not ok_exp(res):
raise CAPIError(f"{func.__name__} returned {res}")
raise CAPIError(func.__name__, res)
elif not ok_exp(res) and res not in ok:
raise CAPIError(func.__name__, res)
return res
except CAPIError as e:
self.logger_cbindings.exception(f"{type(e).__name__}: {e}")
self.logger_cbindings.exception(str(e))
raise

View File

@ -5,6 +5,15 @@ class InstallError(Exception):
class CAPIError(Exception):
"""Exception raised when the C-API returns error values"""
def __init__(self, fn_name, code, msg=None):
self.fn_name = fn_name
self.code = code
self.message = msg if msg else f"{fn_name} returned {code}"
super().__init__(self.message)
def __str__(self):
return f"{type(self).__name__}: {self.message}"
class VMError(Exception):
"""Exception raised when general errors occur"""