This is an automated email from the ASF dual-hosted git repository.

sbp pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tooling-trusted-release.git


The following commit(s) were added to refs/heads/main by this push:
     new 59649a7  Fix problems with returning and reading errors from the API
59649a7 is described below

commit 59649a7bbd14f55c6c35d5ed15513f350fe28284
Author: Sean B. Palmer <[email protected]>
AuthorDate: Mon Jul 7 15:11:59 2025 +0100

    Fix problems with returning and reading errors from the API
---
 atr/blueprints/api/__init__.py |  5 +++++
 atr/server.py                  | 12 ++++++++++++
 client/atr                     |  4 ++--
 3 files changed, 19 insertions(+), 2 deletions(-)

diff --git a/atr/blueprints/api/__init__.py b/atr/blueprints/api/__init__.py
index f330c4c..3c34d20 100644
--- a/atr/blueprints/api/__init__.py
+++ b/atr/blueprints/api/__init__.py
@@ -45,6 +45,11 @@ async def _handle_http_exception(err: 
exceptions.HTTPException) -> tuple[quart.R
     return _json_error(err.description or err.name, err.code)
 
 
[email protected](exceptions.NotFound)
+async def _handle_not_found(err: exceptions.NotFound) -> tuple[quart.Response, 
int]:
+    return _json_error(err.description or err.name, 404)
+
+
 def _json_error(message: str, status_code: int | None) -> 
tuple[quart.Response, int]:
     return quart.jsonify({"error": message}), status_code or 500
 
diff --git a/atr/server.py b/atr/server.py
index c025a7f..62abdb9 100644
--- a/atr/server.py
+++ b/atr/server.py
@@ -28,6 +28,7 @@ import asfquart.base as base
 import asfquart.generics
 import asfquart.session
 import blockbuster
+import quart
 import quart_schema
 import quart_wtf
 import rich.logging as rich_logging
@@ -245,6 +246,11 @@ def register_routes(app: base.QuartApp) -> ModuleType:
     async def handle_any_exception(error: Exception) -> Any:
         import traceback
 
+        # If the request was made to the API, return JSON
+        if quart.request.path.startswith("/api"):
+            status_code = getattr(error, "code", 500) if isinstance(error, 
Exception) else 500
+            return quart.jsonify({"error": str(error)}), status_code
+
         # Required to give to the error.html template
         tb = traceback.format_exc()
         app.logger.exception("Unhandled exception")
@@ -253,6 +259,9 @@ def register_routes(app: base.QuartApp) -> ModuleType:
     @app.errorhandler(base.ASFQuartException)
     async def handle_asfquart_exception(error: base.ASFQuartException) -> Any:
         # TODO: Figure out why pyright doesn't know about this attribute
+        if quart.request.path.startswith("/api"):
+            errorcode = getattr(error, "errorcode", 500)
+            return quart.jsonify({"error": str(error)}), errorcode
         if not hasattr(error, "errorcode"):
             errorcode = 500
         else:
@@ -262,6 +271,9 @@ def register_routes(app: base.QuartApp) -> ModuleType:
     # Add a global error handler in case a page does not exist.
     @app.errorhandler(404)
     async def handle_not_found(error: Exception) -> Any:
+        # Serve JSON for API endpoints, HTML otherwise
+        if quart.request.path.startswith("/api"):
+            return quart.jsonify({"error": "404 Not Found"}), 404
         return await template.render("notfound.html", error="404 Not Found", 
traceback="", status_code=404), 404
 
     return modules
diff --git a/client/atr b/client/atr
index 1c22188..b6ded47 100755
--- a/client/atr
+++ b/client/atr
@@ -543,8 +543,8 @@ async def web_get(url: str, jwt_token: str, verify_ssl: 
bool = True) -> Any:
                 text = await resp.text()
                 try:
                     error_data = json.loads(text)
-                    if isinstance(error_data, dict) and "message" in 
error_data:
-                        LOGGER.error(error_data["message"])
+                    if isinstance(error_data, dict) and "error" in error_data:
+                        LOGGER.error(error_data["error"])
                     else:
                         LOGGER.error(f"Request failed: {resp.status} {text}")
                 except json.JSONDecodeError:


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to