home · contact · privacy
Display HandledException as HTML message with appropriate status code.
authorChristian Heller <c.heller@plomlompom.de>
Sat, 16 Mar 2024 23:36:22 +0000 (00:36 +0100)
committerChristian Heller <c.heller@plomlompom.de>
Sat, 16 Mar 2024 23:36:22 +0000 (00:36 +0100)
task.py
templates/base.html

diff --git a/task.py b/task.py
index 0b8825476353fff066f4b5480c7fcbf2d5ef00b5..3af1cacadc4c30700ef18c748ff81ee9f9a01ed2 100755 (executable)
--- a/task.py
+++ b/task.py
@@ -18,22 +18,29 @@ class HandledException(Exception):
 class TaskHandler(BaseHTTPRequestHandler):
     """Handles single HTTP request."""
 
-    def do_GET(self):
-        """Handle any GET request."""
-        parsed_url = urlparse(self.path)
-        site = path_split(parsed_url.path)[1]
-        if 'calendar' == site:
-            html = self.do_GET_calendar()
-        else:
-            html = self.do_GET_msg('I have nothing to say at this point.')
-        self.send_response(200)
+    def send_html(self, html, code=200):
+        """Send HTML as proper HTTP response."""
+        self.send_response(code)
         self.end_headers()
         self.wfile.write(bytes(html, 'utf-8'))
 
-    def do_GET_msg(self, msg):
-        """Show message."""
-        return self.server.jinja.get_template('msg.html').render(
-                msg=msg)
+    def send_msg(self, msg, code=400):
+        """Send message in HTML formatting as HTTP response."""
+        html = self.server.jinja.get_template('msg.html').render(msg=msg)
+        self.send_html(html, code)
+
+    def do_GET(self):
+        """Handle any GET request."""
+        try:
+            parsed_url = urlparse(self.path)
+            site = path_split(parsed_url.path)[1]
+            if 'calendar' == site:
+                html = self.do_GET_calendar()
+            else:
+                raise HandledException('Test!')
+            self.send_html(html)
+        except HandledException as error:
+            self.send_msg(error)
 
     def do_GET_calendar(self):
         """Show sorted Days."""
index 427740bf4318da2cdcdd7416cd0e57d9ab9b22f7..a3975e46833632b52e3f2d1ac21889b240e88b51 100644 (file)
@@ -2,7 +2,7 @@
 <html>
 <meta charset="UTF-8">
 <body>
-<a href="calendar">calendar</a> | <a href="test">test</a>
+<a href="calendar">calendar</a> | <a href="error">error</a>
 <hr>
 {% block content %}
 {% endblock %}