← Back to team overview

canonical-ubuntu-qa team mailing list archive

[Merge] ~hyask/autopkgtest-cloud:skia/browse_exception_handler into autopkgtest-cloud:master

 

Skia has proposed merging ~hyask/autopkgtest-cloud:skia/browse_exception_handler into autopkgtest-cloud:master.

Requested reviews:
  Canonical's Ubuntu QA (canonical-ubuntu-qa)

For more details, see:
https://code.launchpad.net/~hyask/autopkgtest-cloud/+git/autopkgtest-cloud/+merge/461059
-- 
Your team Canonical's Ubuntu QA is requested to review the proposed merge of ~hyask/autopkgtest-cloud:skia/browse_exception_handler into autopkgtest-cloud:master.
diff --git a/charms/focal/autopkgtest-web/webcontrol/browse.cgi b/charms/focal/autopkgtest-web/webcontrol/browse.cgi
index f6be794..88f4090 100755
--- a/charms/focal/autopkgtest-web/webcontrol/browse.cgi
+++ b/charms/focal/autopkgtest-web/webcontrol/browse.cgi
@@ -12,6 +12,7 @@ from wsgiref.handlers import CGIHandler
 
 import flask
 from helpers.admin import select_abnormally_long_jobs
+from helpers.exceptions import RunningJSONNotFound
 from helpers.utils import get_all_releases, get_supported_releases
 from werkzeug.middleware.proxy_fix import ProxyFix
 
@@ -62,10 +63,9 @@ def get_running_jobs():
     try:
         with open(RUNNING_CACHE) as f:
             # package -> runhash -> release -> arch -> (params, duration, logtail)
-            running_info = json.load(f)
-    except FileNotFoundError:
-        running_info = {}
-    return running_info
+            return json.load(f)
+    except FileNotFoundError as e:
+        raise RunningJSONNotFound(e) from e
 
 
 def render(template, code=200, **kwargs):
@@ -90,7 +90,7 @@ def render(template, code=200, **kwargs):
             template,
             base_url=flask.url_for("index_root"),
             static_url=flask.url_for("static", filename=""),
-            **kwargs
+            **kwargs,
         ),
         code,
     )
@@ -521,6 +521,25 @@ def statistics():
     )
 
 
+def invalid(inv_exception, code=400):
+    """Return message and HTTP error code for an invalid request"""
+    return render("browse-error.html", error=inv_exception, code=code)
+
+
+@app.errorhandler(Exception)
+def all_exception_handler(error):
+    # If the exception doesn't have the exit_code method, it's not an expected
+    # exception defined in helpers/exceptions.py
+    try:
+        return invalid(error, error.exit_code())
+    except Exception as e:
+        return render(
+            "browse-error.html",
+            error=f"Error {e} during handling of {error}",
+            code=500,
+        )
+
+
 if __name__ == "__main__":
     app.config["DEBUG"] = True
     init_config()
diff --git a/charms/focal/autopkgtest-web/webcontrol/helpers/exceptions.py b/charms/focal/autopkgtest-web/webcontrol/helpers/exceptions.py
index becb8c7..ac58625 100644
--- a/charms/focal/autopkgtest-web/webcontrol/helpers/exceptions.py
+++ b/charms/focal/autopkgtest-web/webcontrol/helpers/exceptions.py
@@ -10,6 +10,14 @@ EXAMPLE_URL = (
 )
 
 
+class RunningJSONNotFound(FileNotFoundError):
+    def __init__(self, message):
+        super().__init__(message)
+
+    def exit_code(self):
+        return 500
+
+
 class WebControlException(Exception):
     def __init__(self, message, exit_code):
         super().__init__(message)
diff --git a/charms/focal/autopkgtest-web/webcontrol/templates/browse-error.html b/charms/focal/autopkgtest-web/webcontrol/templates/browse-error.html
index af23654..85e2a42 100644
--- a/charms/focal/autopkgtest-web/webcontrol/templates/browse-error.html
+++ b/charms/focal/autopkgtest-web/webcontrol/templates/browse-error.html
@@ -1,5 +1,12 @@
 {% extends "browse-layout.html" %}
 {% block content %}
-<h1>Error:</h1>
-<p>{{error}}</p>
+<div style="background: #faa; padding: 0.5em 1.5em;">
+  <h1>Error:</h1>
+  <p>{{ error }}</p>
+</div>
+<p>
+A server error has occured, please contact a member of the Ubuntu QA team. You
+can contact them via the ubuntu-quality mailing list, or via the #ubuntu-quality
+IRC channel on irc.libera.chat (highlight 'qa-help' for more reactivity).
+</p>
 {% endblock %}

Follow ups