diff options
author | Alexander_Kabui | 2024-01-26 13:36:49 +0300 |
---|---|---|
committer | Alexander_Kabui | 2024-01-26 13:36:49 +0300 |
commit | 48ccd489508fcb2f6285561902826025b0c4e74c (patch) | |
tree | 00f28cac5b5c0cfaa3fb076b81ccf7f10f634c23 /gn3 | |
parent | cf51516b6f9d673c2ad9ec35604d25b831c48b5f (diff) | |
download | genenetwork3-48ccd489508fcb2f6285561902826025b0c4e74c.tar.gz |
centralize common errors
Diffstat (limited to 'gn3')
-rw-r--r-- | gn3/api/llm.py | 52 |
1 files changed, 28 insertions, 24 deletions
diff --git a/gn3/api/llm.py b/gn3/api/llm.py index 60a213a..98ff4e2 100644 --- a/gn3/api/llm.py +++ b/gn3/api/llm.py @@ -3,6 +3,8 @@ # pylint: skip-file from flask import jsonify, request, Blueprint, current_app + +from functools import wraps from gn3.auth.authorisation.oauth2.resource_server import require_oauth from gn3.llms.process import get_gnqa @@ -17,6 +19,16 @@ from datetime import timedelta GnQNA = Blueprint("GnQNA", __name__) +def handle_errors(func): + @wraps(func) + def decorated_function(*args, **kwargs): + try: + return func(*args, **kwargs) + except Exception as error: + return jsonify({"error": str(error)}), 500 + return decorated_function + + @GnQNA.route("/gnqna", methods=["POST"]) def gnqa(): query = request.json.get("querygnqa", "") @@ -63,40 +75,32 @@ def rating(task_id, doc_id, rating): @GnQNA.route("/history/<query>", methods=["GET"]) @require_oauth("profile user") +@handle_errors def fetch_user_hist(query): - try: - - with (require_oauth.acquire("profile user") as the_token, Redis.from_url(current_app.config["REDIS_URI"], - decode_responses=True) as redis_conn): - return jsonify({ - **fetch_query_results(query, the_token.user.id, redis_conn), - "prev_queries": get_user_queries("random_user", redis_conn) - }) - except Exception as error: - return jsonify({"error": str(error)}), 500 + with (require_oauth.acquire("profile user") as the_token, Redis.from_url(current_app.config["REDIS_URI"], + decode_responses=True) as redis_conn): + return jsonify({ + **fetch_query_results(query, the_token.user.id, redis_conn), + "prev_queries": get_user_queries("random_user", redis_conn) + }) @GnQNA.route("/historys/<query>", methods=["GET"]) +@handle_errors def fetch_users_hist_records(query): """method to fetch all users hist:note this is a test functionality to be replaced by fetch_user_hist""" - try: - - with Redis.from_url(current_app.config["REDIS_URI"], decode_responses=True) as redis_conn: - return jsonify({ - **fetch_query_results(query, "random_user", redis_conn), - "prev_queries": get_user_queries("random_user", redis_conn) - }) - except Exception as error: - return jsonify({"error": str(error)}), 500 + with Redis.from_url(current_app.config["REDIS_URI"], decode_responses=True) as redis_conn: + return jsonify({ + **fetch_query_results(query, "random_user", redis_conn), + "prev_queries": get_user_queries("random_user", redis_conn) + }) @GnQNA.route("/get_hist_names", methods=["GET"]) +@handle_errors def fetch_prev_hist_ids(): - try: - with (Redis.from_url(current_app.config["REDIS_URI"], decode_responses=True)) as redis_conn: - return jsonify({"prev_queries": get_user_queries("random_user", redis_conn)}) - except Exception as error: - return jsonify({"error": str(error)}), 500 + with (Redis.from_url(current_app.config["REDIS_URI"], decode_responses=True)) as redis_conn: + return jsonify({"prev_queries": get_user_queries("random_user", redis_conn)}) |