diff options
author | Alexander_Kabui | 2024-05-22 13:37:32 +0300 |
---|---|---|
committer | Alexander_Kabui | 2024-05-22 13:37:32 +0300 |
commit | 12100489a73094016602926183e0ee51002fb9c6 (patch) | |
tree | 7b8e4c56917b1fe02cf1c1da29aa8849dd9df79a /gn3 | |
parent | 2a99da9f46233a28e9ea0b6a297d8a6b93f61923 (diff) | |
download | genenetwork3-12100489a73094016602926183e0ee51002fb9c6.tar.gz |
Register LLM error in app.
* do refactoring for gn3:llm:errors
Diffstat (limited to 'gn3')
-rw-r--r-- | gn3/errors.py | 17 | ||||
-rw-r--r-- | gn3/llms/errors.py | 14 |
2 files changed, 25 insertions, 6 deletions
diff --git a/gn3/errors.py b/gn3/errors.py index 1833bf6..ac9e070 100644 --- a/gn3/errors.py +++ b/gn3/errors.py @@ -16,7 +16,7 @@ from authlib.oauth2.rfc6749.errors import OAuth2Error from flask import Flask, jsonify, Response, current_app from gn3.auth.authorisation.errors import AuthorisationError - +from gn3.llms.errors import LLMError def add_trace(exc: Exception, jsonmsg: dict) -> dict: """Add the traceback to the error handling object.""" @@ -106,6 +106,20 @@ def handle_generic(exc: Exception) -> Response: return resp +def handle_llm_error(exc: Exception) -> Response: + """ Handle llm erros if not handled anywhere else. """ + resp = jsonify({ + "query": exc.query if exc.query else "", + "error_type": type(exc).__name__, + "error": ( + exc.args if bool(exc.args) else "Fahamu gnqa error occurred" + ), + "trace": traceback.format_exc() + }) + resp.status_code = 500 + return resp + + def register_error_handlers(app: Flask): """Register application-level error handlers.""" app.register_error_handler(NotFound, page_not_found) @@ -115,6 +129,7 @@ def register_error_handlers(app: Flask): app.register_error_handler(AuthorisationError, handle_authorisation_error) app.register_error_handler(RemoteDisconnected, internal_server_error) app.register_error_handler(URLError, url_server_error) + app.register_error_handler(LLMError, handle_llm_error) for exc in ( EndPointInternalError, EndPointNotFound, diff --git a/gn3/llms/errors.py b/gn3/llms/errors.py index af3d7b0..3512f4d 100644 --- a/gn3/llms/errors.py +++ b/gn3/llms/errors.py @@ -35,8 +35,12 @@ class UnprocessableEntity(HTTPError): msg, request=request, response=response) -class LLMError(HTTPError): - """Custom error from making Fahamu APi request """ - def __init__(self, request, response, msg): - super(HTTPError, self).__init__( - msg, request=request, response=response) +class LLMErrorMIxins(Exception): + """base class for llm errors""" + + +class LLMError(LLMErrorMIxins): + """custom exception for LLMErrorMIxins""" + def __init__(self, *args, **kwargs): + super().__init__(*args) + self.query = kwargs.get("query") |