From bafaa585074c02142e15e38dcc66de385b04ca12 Mon Sep 17 00:00:00 2001 From: onyx-and-iris Date: Mon, 10 Jul 2023 15:36:38 +0100 Subject: [PATCH] extends error class now accepts a custom message fn_name and error code stored as class attributes --- voicemeeterlib/cbindings.py | 10 +++++----- voicemeeterlib/error.py | 9 +++++++++ 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/voicemeeterlib/cbindings.py b/voicemeeterlib/cbindings.py index 87413a9..cd2cc4f 100644 --- a/voicemeeterlib/cbindings.py +++ b/voicemeeterlib/cbindings.py @@ -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 diff --git a/voicemeeterlib/error.py b/voicemeeterlib/error.py index 11aeb09..f51e4e1 100644 --- a/voicemeeterlib/error.py +++ b/voicemeeterlib/error.py @@ -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"""