Fix bug in API exception handling

This commit is contained in:
krateng 2024-02-24 19:15:51 +01:00
parent 738f42d49f
commit 163746c06e

View File

@ -33,8 +33,12 @@ cla = CleanerAgent()
class APIHandler: class APIHandler:
__apiname__: str
errors: dict
# make these classes singletons # make these classes singletons
_instance = None _instance = None
def __new__(cls, *args, **kwargs): def __new__(cls, *args, **kwargs):
if not isinstance(cls._instance, cls): if not isinstance(cls._instance, cls):
cls._instance = object.__new__(cls, *args, **kwargs) cls._instance = object.__new__(cls, *args, **kwargs)
@ -69,17 +73,16 @@ class APIHandler:
try: try:
response.status,result = self.handle(path,keys) response.status,result = self.handle(path,keys)
except Exception: except Exception as e:
exceptiontype = sys.exc_info()[0]
for exc_type, exc_response in self.errors.items(): for exc_type, exc_response in self.errors.items():
if isinstance(exceptiontype, exc_type): if isinstance(e, exc_type):
response.status, result = exc_response 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 break
else: else:
# THIS SHOULD NOT HAPPEN # THIS SHOULD NOT HAPPEN
response.status, result = 500, {"status": "Unknown error", "code": 500} 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 return result