From df473d89ae737c5b54bdd1063c8de8735b7ba172 Mon Sep 17 00:00:00 2001 From: onyx-and-iris Date: Sun, 13 Aug 2023 16:50:58 +0100 Subject: [PATCH] remove __str__ override in VMError move error message for code -9 into CAPIError class --- voicemeeterlib/cbindings.py | 2 +- voicemeeterlib/error.py | 20 +++++++++++--------- voicemeeterlib/remote.py | 24 +++--------------------- 3 files changed, 15 insertions(+), 31 deletions(-) diff --git a/voicemeeterlib/cbindings.py b/voicemeeterlib/cbindings.py index e4e64ff..ab6df28 100644 --- a/voicemeeterlib/cbindings.py +++ b/voicemeeterlib/cbindings.py @@ -121,5 +121,5 @@ class CBindings(metaclass=ABCMeta): raise CAPIError(func.__name__, res) return res except CAPIError as e: - self.logger_cbindings.exception(str(e)) + self.logger_cbindings.exception(f"{type(e).__name__}: {e}") raise diff --git a/voicemeeterlib/error.py b/voicemeeterlib/error.py index 10be769..53dd424 100644 --- a/voicemeeterlib/error.py +++ b/voicemeeterlib/error.py @@ -1,13 +1,6 @@ class VMError(Exception): """Base VM Exception class. Raised when general errors occur.""" - def __init__(self, msg): - self.message = msg - super().__init__(self.message) - - def __str__(self): - return f"{type(self).__name__}: {self.message}" - class InstallError(VMError): """Exception raised when installation errors occur""" @@ -16,7 +9,16 @@ class InstallError(VMError): class CAPIError(VMError): """Exception raised when the C-API returns an error code""" - def __init__(self, fn_name, code, msg=None): + def __init__(self, fn_name, code): self.fn_name = fn_name self.code = code - super(CAPIError, self).__init__(msg if msg else f"{fn_name} returned {code}") + if self.code == -9: + message = " ".join( + ( + f"no bind for {self.fn_name}.", + "are you using an old version of the API?", + ) + ) + else: + message = f"{self.fn_name} returned {self.code}" + super().__init__(message) diff --git a/voicemeeterlib/remote.py b/voicemeeterlib/remote.py index 51f7bc9..d9617ac 100644 --- a/voicemeeterlib/remote.py +++ b/voicemeeterlib/remote.py @@ -121,13 +121,7 @@ class Remote(CBindings): return self.call(self.bind_macro_button_is_dirty, ok=(0, 1)) == 1 except AttributeError as e: self.logger.exception(f"{type(e).__name__}: {e}") - ERR_MSG = ( - "no bind for VBVMR_MacroButton_IsDirty.", - "are you using an old version of the API?", - ) - raise CAPIError( - "VBVMR_MacroButton_IsDirty", -9, msg=" ".join(ERR_MSG) - ) from e + raise CAPIError("VBVMR_MacroButton_IsDirty", -9) from e @property def ldirty(self) -> bool: @@ -187,13 +181,7 @@ class Remote(CBindings): ) except AttributeError as e: self.logger.exception(f"{type(e).__name__}: {e}") - ERR_MSG = ( - "no bind for VBVMR_MacroButton_GetStatus.", - "are you using an old version of the API?", - ) - raise CAPIError( - "VBVMR_MacroButton_GetStatus", -9, msg=" ".join(ERR_MSG) - ) from e + raise CAPIError("VBVMR_MacroButton_GetStatus", -9) from e return int(c_state.value) def set_buttonstatus(self, id_: int, val: int, mode: int) -> None: @@ -208,13 +196,7 @@ class Remote(CBindings): ) except AttributeError as e: self.logger.exception(f"{type(e).__name__}: {e}") - ERR_MSG = ( - "no bind for VBVMR_MacroButton_SetStatus.", - "are you using an old version of the API?", - ) - raise CAPIError( - "VBVMR_MacroButton_SetStatus", -9, msg=" ".join(ERR_MSG) - ) from e + raise CAPIError("VBVMR_MacroButton_SetStatus", -9) from e self.cache[f"mb_{id_}_{mode}"] = int(c_state.value) def get_num_devices(self, direction: str = None) -> int: