about summary refs log tree commit diff
path: root/gn3/errors.py
diff options
context:
space:
mode:
Diffstat (limited to 'gn3/errors.py')
-rw-r--r--gn3/errors.py42
1 files changed, 27 insertions, 15 deletions
diff --git a/gn3/errors.py b/gn3/errors.py
index c53604f..ec7a554 100644
--- a/gn3/errors.py
+++ b/gn3/errors.py
@@ -15,6 +15,7 @@ from werkzeug.exceptions import NotFound
 from authlib.oauth2.rfc6749.errors import OAuth2Error
 from flask import Flask, jsonify, Response, current_app
 
+from gn3.oauth2 import errors as oautherrors
 from gn3.auth.authorisation.errors import AuthorisationError
 from  gn3.llms.errors import LLMError
 
@@ -28,6 +29,7 @@ def add_trace(exc: Exception, jsonmsg: dict) -> dict:
 
 def page_not_found(pnf):
     """Generic 404 handler."""
+    current_app.logger.error("Handling 404 errors", exc_info=True)
     return jsonify(add_trace(pnf, {
         "error": pnf.name,
         "error_description": pnf.description
@@ -36,6 +38,7 @@ def page_not_found(pnf):
 
 def internal_server_error(pnf):
     """Generic 404 handler."""
+    current_app.logger.error("Handling internal server errors", exc_info=True)
     return jsonify(add_trace(pnf, {
         "error": pnf.name,
         "error_description": pnf.description
@@ -44,15 +47,16 @@ def internal_server_error(pnf):
 
 def url_server_error(pnf):
     """Handler for an exception with a url connection."""
+    current_app.logger.error("Handling url server errors", exc_info=True)
     return jsonify(add_trace(pnf, {
         "error": f"URLLib Error no: {pnf.reason.errno}",
         "error_description": pnf.reason.strerror,
-    }))
+    })), 500
 
 
 def handle_authorisation_error(exc: AuthorisationError):
     """Handle AuthorisationError if not handled anywhere else."""
-    current_app.logger.error(exc)
+    current_app.logger.error("Handling external auth errors", exc_info=True)
     return jsonify(add_trace(exc, {
         "error": type(exc).__name__,
         "error_description": " :: ".join(exc.args)
@@ -61,7 +65,7 @@ def handle_authorisation_error(exc: AuthorisationError):
 
 def handle_oauth2_errors(exc: OAuth2Error):
     """Handle OAuth2Error if not handled anywhere else."""
-    current_app.logger.error(exc)
+    current_app.logger.error("Handling external oauth2 errors", exc_info=True)
     return jsonify(add_trace(exc, {
         "error": exc.error,
         "error_description": exc.description,
@@ -70,7 +74,7 @@ def handle_oauth2_errors(exc: OAuth2Error):
 
 def handle_sqlite3_errors(exc: OperationalError):
     """Handle sqlite3 errors if not handled anywhere else."""
-    current_app.logger.error(exc)
+    current_app.logger.error("Handling sqlite3 errors", exc_info=True)
     return jsonify({
         "error": "DatabaseError",
         "error_description": exc.args[0],
@@ -78,24 +82,23 @@ def handle_sqlite3_errors(exc: OperationalError):
 
 
 def handle_sparql_errors(exc):
-    """Handle sqlite3 errors if not handled anywhere else."""
-    current_app.logger.error(exc)
-    __code = {
-        EndPointInternalError: 500,
-        EndPointNotFound: 400,
-        QueryBadFormed: 400,
-        Unauthorized: 401,
-        URITooLong: 414,
+    """Handle sparql/virtuoso errors if not handled anywhere else."""
+    current_app.logger.error("Handling sparql errors", exc_info=True)
+    code = {
+        "EndPointInternalError": 500,
+        "EndPointNotFound": 404,
+        "QueryBadFormed": 400,
+        "Unauthorized": 401,
+        "URITooLong": 414,
     }
     return jsonify({
         "error": exc.msg,
-        "error_description": str(exc),
-    }), __code.get(exc)
+    }), code.get(exc.__class__.__name__)
 
 
 def handle_generic(exc: Exception) -> Response:
     """Handle generic exception."""
-    current_app.logger.error(exc)
+    current_app.logger.error("Handling generic errors", exc_info=True)
     resp = jsonify({
         "error": type(exc).__name__,
         "error_description": (
@@ -106,6 +109,15 @@ def handle_generic(exc: Exception) -> Response:
     return resp
 
 
+def handle_local_authorisation_errors(exc: oautherrors.AuthorisationError):
+    """Handle errors relating to authorisation that are raised locally."""
+    current_app.logger.error("Handling local auth errors", exc_info=True)
+    return jsonify(add_trace(exc, {
+        "error": type(exc).__name__,
+        "error_description": " ".join(exc.args)
+    })), 400
+
+
 def handle_llm_error(exc: Exception) -> Response:
     """ Handle llm erros if not handled  anywhere else. """
     current_app.logger.error(exc)