From 163746c06e2e071445b065eab2e00f0e9fab6d23 Mon Sep 17 00:00:00 2001 From: krateng Date: Sat, 24 Feb 2024 19:15:51 +0100 Subject: [PATCH] Fix bug in API exception handling --- maloja/apis/_base.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/maloja/apis/_base.py b/maloja/apis/_base.py index d230cad..61d75bb 100644 --- a/maloja/apis/_base.py +++ b/maloja/apis/_base.py @@ -33,8 +33,12 @@ cla = CleanerAgent() class APIHandler: + + __apiname__: str + errors: dict # make these classes singletons _instance = None + def __new__(cls, *args, **kwargs): if not isinstance(cls._instance, cls): cls._instance = object.__new__(cls, *args, **kwargs) @@ -69,17 +73,16 @@ class APIHandler: try: response.status,result = self.handle(path,keys) - except Exception: - exceptiontype = sys.exc_info()[0] + except Exception as e: for exc_type, exc_response in self.errors.items(): - if isinstance(exceptiontype, exc_type): + if isinstance(e, exc_type): response.status, result = exc_response - log(f"Error with {self.__apiname__} API: {exceptiontype} (Request: {path})") + log(f"Error with {self.__apiname__} API: {e} (Request: {path})") break else: # THIS SHOULD NOT HAPPEN response.status, result = 500, {"status": "Unknown error", "code": 500} - log(f"Unhandled Exception with {self.__apiname__} API: {exceptiontype} (Request: {path})") + log(f"Unhandled Exception with {self.__apiname__} API: {e} (Request: {path})") return result