[New Feature] Rest API implementation to showcase the OpenDBM features
This commit is contained in:
47
rest_api/app/utils/service_result.py
Normal file
47
rest_api/app/utils/service_result.py
Normal file
@@ -0,0 +1,47 @@
|
||||
from loguru import logger
|
||||
import inspect
|
||||
|
||||
from utils.app_exceptions import AppExceptionCase
|
||||
|
||||
|
||||
class ServiceResult(object):
|
||||
def __init__(self, arg):
|
||||
if isinstance(arg, AppExceptionCase):
|
||||
self.success = False
|
||||
self.exception_case = arg.exception_case
|
||||
self.status_code = arg.status_code
|
||||
else:
|
||||
self.success = True
|
||||
self.exception_case = None
|
||||
self.status_code = None
|
||||
self.value = arg
|
||||
|
||||
def __str__(self):
|
||||
if self.success:
|
||||
return "[Success]"
|
||||
return f'[Exception] "{self.exception_case}"'
|
||||
|
||||
def __repr__(self):
|
||||
if self.success:
|
||||
return "<ServiceResult Success>"
|
||||
return f"<ServiceResult AppException {self.exception_case}>"
|
||||
|
||||
def __enter__(self):
|
||||
return self.value
|
||||
|
||||
def __exit__(self, *kwargs):
|
||||
pass
|
||||
|
||||
|
||||
def caller_info() -> str:
|
||||
info = inspect.getframeinfo(inspect.stack()[2][0])
|
||||
return f"{info.filename}:{info.function}:{info.lineno}"
|
||||
|
||||
|
||||
def handle_result(result: ServiceResult):
|
||||
if not result.success:
|
||||
with result as exception:
|
||||
logger.error(f"{exception} | caller={caller_info()}")
|
||||
raise exception
|
||||
with result as result:
|
||||
return result
|
||||
Reference in New Issue
Block a user