From c9a64a2d925053199ce7257362f9686b2e585398 Mon Sep 17 00:00:00 2001 From: Luis Aleixo Date: Mon, 17 Jul 2023 17:24:23 +0200 Subject: [PATCH] handled exceptions --- caimira/apps/calculator/__init__.py | 12 ++++--- caimira/apps/calculator/data_service.py | 43 +++++++++++-------------- 2 files changed, 25 insertions(+), 30 deletions(-) diff --git a/caimira/apps/calculator/__init__.py b/caimira/apps/calculator/__init__.py index f57449f5..b3954128 100644 --- a/caimira/apps/calculator/__init__.py +++ b/caimira/apps/calculator/__init__.py @@ -38,7 +38,7 @@ from .user import AuthenticatedUser, AnonymousUser # calculator version. If the calculator needs to make breaking changes (e.g. change # form attributes) then it can also increase its MAJOR version without needing to # increase the overall CAiMIRA version (found at ``caimira.__version__``). -__version__ = "4.11" +__version__ = "4.12" LOG = logging.getLogger(__name__) @@ -107,13 +107,15 @@ class ConcentrationModel(BaseRequestHandler): start = datetime.datetime.now() # Data Service API Integration + data_service: DataService = self.settings["data_service"] try: - data_service: DataService = self.settings["data_service"] access_token = await data_service.login() service_data = await data_service.fetch(access_token) - except Exception as e: - print("Something went wrong with the data service: %s" % e) - + except Exception as err: + error_message = f"Something went wrong with the data service: {str(err)}" + LOG.error(error_message, exc_info=True) + self.send_error(500, reason=error_message) + try: form = model_generator.FormData.from_dict(requested_model_config) except Exception as err: diff --git a/caimira/apps/calculator/data_service.py b/caimira/apps/calculator/data_service.py index 4ac27fa1..2d480780 100644 --- a/caimira/apps/calculator/data_service.py +++ b/caimira/apps/calculator/data_service.py @@ -2,14 +2,13 @@ import dataclasses import json import logging -from tornado.web import RequestHandler from tornado.httpclient import AsyncHTTPClient, HTTPRequest LOG = logging.getLogger(__name__) @dataclasses.dataclass -class DataService(RequestHandler): +class DataService(): ''' Responsible for establishing a connection to a database through a REST API by handling authentication @@ -27,24 +26,20 @@ class DataService(RequestHandler): client_password = self.credentials['data_service_client_password'] if (client_email == None or client_password == None): - # If the credentials are not defined, an error is thrown. - return self.send_error(500) + # If the credentials are not defined, an exception is raised. + raise Exception("DataService credentials not set") http_client = AsyncHTTPClient() headers = {'Content-type': 'application/json'} json_body = { "email": f"{client_email}", "password": f"{client_password}"} - try: - response = await http_client.fetch(HTTPRequest( - url=self.host + '/login', - method='POST', - headers=headers, - body=json.dumps(json_body), - ), - raise_error=True) - except Exception as err: - LOG.error("Something went wrong: %s" % err) - self.send_error(500) + response = await http_client.fetch(HTTPRequest( + url=self.host + '/login', + method='POST', + headers=headers, + body=json.dumps(json_body), + ), + raise_error=True) return json.loads(response.body)['access_token'] @@ -52,14 +47,12 @@ class DataService(RequestHandler): http_client = AsyncHTTPClient() headers = {'Authorization': f'Bearer {access_token}'} - try: - response = await http_client.fetch(HTTPRequest( - url=self.host + '/data', - method='GET', - headers=headers, - ), - raise_error=True) - except Exception as e: - print("Something went wrong: %s" % e) + response = await http_client.fetch(HTTPRequest( + url=self.host + '/data', + method='GET', + headers=headers, + ), + raise_error=True) - return json.loads(response.body) \ No newline at end of file + return json.loads(response.body) + \ No newline at end of file