From a3d0635bae637f88dd4a2a3f0ba1411321c39412 Mon Sep 17 00:00:00 2001 From: Matthew Stratford Date: Thu, 16 Sep 2021 23:40:05 +0100 Subject: [PATCH] Fix 404 for logs pages, add 500 error page. --- ui-templates/404.html | 9 --------- ui-templates/error.html | 9 +++++++++ web_server.py | 17 +++++++++++++---- 3 files changed, 22 insertions(+), 13 deletions(-) delete mode 100644 ui-templates/404.html create mode 100644 ui-templates/error.html diff --git a/ui-templates/404.html b/ui-templates/404.html deleted file mode 100644 index 5c3bae0..0000000 --- a/ui-templates/404.html +++ /dev/null @@ -1,9 +0,0 @@ -{% extends 'base.html' %} -{% block content_inner %} -
-
404
-

Page Not Found

-

Looks like you fell off the tip of the iceberg.

- ← Escape Back Home -
-{% endblock %} diff --git a/ui-templates/error.html b/ui-templates/error.html new file mode 100644 index 0000000..00730d2 --- /dev/null +++ b/ui-templates/error.html @@ -0,0 +1,9 @@ +{% extends 'base.html' %} +{% block content_inner %} +
+
{{data.code}}
+

{{data.title}}

+

{{data.message}}

+ ← Escape Back Home +
+{% endblock %} diff --git a/web_server.py b/web_server.py index 60dd29c..85ee75e 100644 --- a/web_server.py +++ b/web_server.py @@ -1,5 +1,5 @@ from sanic import Sanic -from sanic.exceptions import NotFound, abort +from sanic.exceptions import NotFound, ServerError, abort from sanic.response import html, file, redirect from sanic.response import json as resp_json from sanic_cors import CORS @@ -108,9 +108,13 @@ player_from_q: List[Queue] = [] @app.exception(NotFound) def page_not_found(request, e: Any): - data = {"ui_page": "404", "ui_title": "404"} - return render_template("404.html", data=data, status=404) + data = {"ui_page": "404", "ui_title": "404", "code": 404, "title": "Page Not Found", "message": "Looks like you fell off the tip of the iceberg." } + return render_template("error.html", data=data, status=404) +@app.exception(Exception, ServerError) +def server_error(request, e: Exception): + data = {"ui_page": "500", "ui_title": "500", "code": 500, "title": "Something went very wrong!", "message": "Looks like the server fell over. Try viewing the WebServer logs for more details." } + return render_template("error.html", data=data, status=500) @app.route("/") def ui_index(request): @@ -234,7 +238,12 @@ def ui_logs_render(request, path): page = int(page) assert page >= 1 - log_file = open(resolve_external_file_path("/logs/{}.log").format(path)) + try: + log_file = open(resolve_external_file_path("/logs/{}.log").format(path)) + except FileNotFoundError: + abort(404) + return + data = { "logs": log_file.read().splitlines()[ -300 * page:(-300 * (page - 1) if page > 1 else None)