Skip to content

Commit

Permalink
exception handling
Browse files Browse the repository at this point in the history
  • Loading branch information
burnout87 committed Oct 4, 2024
1 parent c81decd commit 0dd8cb5
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 33 deletions.
13 changes: 6 additions & 7 deletions cdci_data_analysis/analysis/ivoa_helper.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os.path
from audioop import error

Check notice

Code scanning / CodeQL

Unused import Note

Import of 'error' is not used.

from queryparser.adql import ADQLQueryTranslator
from queryparser.exceptions import QuerySyntaxError
Expand All @@ -7,6 +8,7 @@

from ..flask_app.sentry import sentry

Check notice

Code scanning / CodeQL

Unused import Note

Import of 'sentry' is not used.
from ..app_logging import app_logging
from ..analysis.exceptions import RequestNotUnderstood

logger = app_logging.getLogger('ivoa_helper')

Expand All @@ -21,11 +23,8 @@ def parse_adql_query(query):
)

except QuerySyntaxError as qe:
logger.error(f'Error parsing ADQL query: {qe}')
output_obj = dict(
mysql_query=None,
psql_query=None
)
logger.error(f'Error while parsing the ADQL query: {str(qe)}')
raise RequestNotUnderstood(f"Error while parsing the ADQL query: {str(qe)}")
return output_obj


Expand Down Expand Up @@ -58,7 +57,7 @@ def run_ivoa_query_from_product_gallery(parsed_query_obj,
product_gallery_url=None
):
result_list = []

connection = None
try:
with connect(
host=vo_psql_pg_host,
Expand All @@ -81,8 +80,8 @@ def run_ivoa_query_from_product_gallery(parsed_query_obj,
result_list.append(list_row)

except (Exception, DatabaseError) as e:
sentry.capture_message(f"Error when querying to the Postgresql server: {str(e)}")
logger.error(f"Error when querying to the Postgresql server: {str(e)}")
raise e

finally:
if connection is not None:
Expand Down
68 changes: 42 additions & 26 deletions cdci_data_analysis/flask_app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,38 +432,54 @@ def push_renku_branch():

@app.route('/run_adql_query')
def run_adql_query():
par_dic = request.values.to_dict()
sanitized_request_values = sanitize_dict_before_log(par_dic)
logger.info('\033[32m===========================> run_adql_query\033[0m')
try:
par_dic = request.values.to_dict()
sanitized_request_values = sanitize_dict_before_log(par_dic)
logger.info('\033[32m===========================> run_adql_query\033[0m')

logger.info('\033[33m raw request values: %s \033[0m', dict(sanitized_request_values))
logger.info('\033[33m raw request values: %s \033[0m', dict(sanitized_request_values))

token = par_dic.get('token', None)
app_config = app.config.get('conf')
secret_key = app_config.secret_key

output, output_code = tokenHelper.validate_token_from_request(token=token, secret_key=secret_key,
required_roles=['ivoa_user'],
action="run an ADQL query")
token = par_dic.get('token', None)
app_config = app.config.get('conf')
secret_key = app_config.secret_key

if output_code is not None:
return make_response(output, output_code)
output, output_code = tokenHelper.validate_token_from_request(token=token, secret_key=secret_key,
required_roles=['ivoa_user'],
action="run an ADQL query")

adql_query = par_dic.get('adql_query', None)
vo_psql_pg_host = app_config.vo_psql_pg_host
vo_psql_pg_user = app_config.vo_psql_pg_user
vo_psql_pg_password = app_config.vo_psql_pg_password
vo_psql_pg_db = app_config.vo_psql_pg_db
product_gallery_url = app_config.product_gallery_url
if output_code is not None:
return make_response(output, output_code)

result_query = ivoa_helper.run_ivoa_query(adql_query,
vo_psql_pg_host=vo_psql_pg_host,
vo_psql_pg_user=vo_psql_pg_user,
vo_psql_pg_password=vo_psql_pg_password,
vo_psql_pg_db=vo_psql_pg_db,
product_gallery_url=product_gallery_url)
adql_query = par_dic.get('adql_query', None)
vo_psql_pg_host = app_config.vo_psql_pg_host
vo_psql_pg_user = app_config.vo_psql_pg_user
vo_psql_pg_password = app_config.vo_psql_pg_password
vo_psql_pg_db = app_config.vo_psql_pg_db
product_gallery_url = app_config.product_gallery_url

result_query = ivoa_helper.run_ivoa_query(adql_query,
vo_psql_pg_host=vo_psql_pg_host,
vo_psql_pg_user=vo_psql_pg_user,
vo_psql_pg_password=vo_psql_pg_password,
vo_psql_pg_db=vo_psql_pg_db,
product_gallery_url=product_gallery_url)

return jsonify(result_query)
except APIerror as api_e:
error_message = f"Error while running an ADQL query: "
if hasattr(api_e, 'message') and api_e.message is not None:
error_message += api_e.message
else:
error_message += f"{repr(api_e)}"
logging.getLogger().error(error_message)
sentry.capture_message(error_message)

return jsonify(result_query)
return make_response(error_message)
except Exception as e:
error_message = f"Error while running an ADQL query: {str(e)}"
logging.getLogger().error(error_message)
sentry.capture_message(error_message)
return make_response(f"Internal error while running an ADQL query. Our team is notified and is working on it.")


@app.route('/run_analysis', methods=['POST', 'GET'])
Expand Down

0 comments on commit 0dd8cb5

Please sign in to comment.