handled exceptions
This commit is contained in:
parent
f9fb2a12fb
commit
c9a64a2d92
2 changed files with 25 additions and 30 deletions
|
|
@ -38,7 +38,7 @@ from .user import AuthenticatedUser, AnonymousUser
|
||||||
# calculator version. If the calculator needs to make breaking changes (e.g. change
|
# 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
|
# form attributes) then it can also increase its MAJOR version without needing to
|
||||||
# increase the overall CAiMIRA version (found at ``caimira.__version__``).
|
# increase the overall CAiMIRA version (found at ``caimira.__version__``).
|
||||||
__version__ = "4.11"
|
__version__ = "4.12"
|
||||||
|
|
||||||
LOG = logging.getLogger(__name__)
|
LOG = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
@ -107,13 +107,15 @@ class ConcentrationModel(BaseRequestHandler):
|
||||||
start = datetime.datetime.now()
|
start = datetime.datetime.now()
|
||||||
|
|
||||||
# Data Service API Integration
|
# Data Service API Integration
|
||||||
|
data_service: DataService = self.settings["data_service"]
|
||||||
try:
|
try:
|
||||||
data_service: DataService = self.settings["data_service"]
|
|
||||||
access_token = await data_service.login()
|
access_token = await data_service.login()
|
||||||
service_data = await data_service.fetch(access_token)
|
service_data = await data_service.fetch(access_token)
|
||||||
except Exception as e:
|
except Exception as err:
|
||||||
print("Something went wrong with the data service: %s" % e)
|
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:
|
try:
|
||||||
form = model_generator.FormData.from_dict(requested_model_config)
|
form = model_generator.FormData.from_dict(requested_model_config)
|
||||||
except Exception as err:
|
except Exception as err:
|
||||||
|
|
|
||||||
|
|
@ -2,14 +2,13 @@ import dataclasses
|
||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from tornado.web import RequestHandler
|
|
||||||
from tornado.httpclient import AsyncHTTPClient, HTTPRequest
|
from tornado.httpclient import AsyncHTTPClient, HTTPRequest
|
||||||
|
|
||||||
LOG = logging.getLogger(__name__)
|
LOG = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
@dataclasses.dataclass
|
@dataclasses.dataclass
|
||||||
class DataService(RequestHandler):
|
class DataService():
|
||||||
'''
|
'''
|
||||||
Responsible for establishing a connection to a
|
Responsible for establishing a connection to a
|
||||||
database through a REST API by handling authentication
|
database through a REST API by handling authentication
|
||||||
|
|
@ -27,24 +26,20 @@ class DataService(RequestHandler):
|
||||||
client_password = self.credentials['data_service_client_password']
|
client_password = self.credentials['data_service_client_password']
|
||||||
|
|
||||||
if (client_email == None or client_password == None):
|
if (client_email == None or client_password == None):
|
||||||
# If the credentials are not defined, an error is thrown.
|
# If the credentials are not defined, an exception is raised.
|
||||||
return self.send_error(500)
|
raise Exception("DataService credentials not set")
|
||||||
|
|
||||||
http_client = AsyncHTTPClient()
|
http_client = AsyncHTTPClient()
|
||||||
headers = {'Content-type': 'application/json'}
|
headers = {'Content-type': 'application/json'}
|
||||||
json_body = { "email": f"{client_email}", "password": f"{client_password}"}
|
json_body = { "email": f"{client_email}", "password": f"{client_password}"}
|
||||||
|
|
||||||
try:
|
response = await http_client.fetch(HTTPRequest(
|
||||||
response = await http_client.fetch(HTTPRequest(
|
url=self.host + '/login',
|
||||||
url=self.host + '/login',
|
method='POST',
|
||||||
method='POST',
|
headers=headers,
|
||||||
headers=headers,
|
body=json.dumps(json_body),
|
||||||
body=json.dumps(json_body),
|
),
|
||||||
),
|
raise_error=True)
|
||||||
raise_error=True)
|
|
||||||
except Exception as err:
|
|
||||||
LOG.error("Something went wrong: %s" % err)
|
|
||||||
self.send_error(500)
|
|
||||||
|
|
||||||
return json.loads(response.body)['access_token']
|
return json.loads(response.body)['access_token']
|
||||||
|
|
||||||
|
|
@ -52,14 +47,12 @@ class DataService(RequestHandler):
|
||||||
http_client = AsyncHTTPClient()
|
http_client = AsyncHTTPClient()
|
||||||
headers = {'Authorization': f'Bearer {access_token}'}
|
headers = {'Authorization': f'Bearer {access_token}'}
|
||||||
|
|
||||||
try:
|
response = await http_client.fetch(HTTPRequest(
|
||||||
response = await http_client.fetch(HTTPRequest(
|
url=self.host + '/data',
|
||||||
url=self.host + '/data',
|
method='GET',
|
||||||
method='GET',
|
headers=headers,
|
||||||
headers=headers,
|
),
|
||||||
),
|
raise_error=True)
|
||||||
raise_error=True)
|
|
||||||
except Exception as e:
|
|
||||||
print("Something went wrong: %s" % e)
|
|
||||||
|
|
||||||
return json.loads(response.body)
|
return json.loads(response.body)
|
||||||
|
|
||||||
Loading…
Reference in a new issue