From 7c7602eae676c9806292ad37c58d22dcce12a14f Mon Sep 17 00:00:00 2001 From: Christian Heller Date: Sun, 17 Mar 2024 00:36:22 +0100 Subject: [PATCH] Display HandledException as HTML message with appropriate status code. --- task.py | 33 ++++++++++++++++++++------------- templates/base.html | 2 +- 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/task.py b/task.py index 0b88254..3af1cac 100755 --- 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.""" diff --git a/templates/base.html b/templates/base.html index 427740b..a3975e4 100644 --- a/templates/base.html +++ b/templates/base.html @@ -2,7 +2,7 @@ -calendar | test +calendar | error
{% block content %} {% endblock %} -- 2.30.2