handled exceptions

This commit is contained in:
Luis Aleixo 2023-07-17 17:24:23 +02:00
parent f9fb2a12fb
commit c9a64a2d92
2 changed files with 25 additions and 30 deletions

View file

@ -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:

View file

@ -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)
return json.loads(response.body)