From 73939a8e2f83b4fc5b648eee39466da1555cf4fd Mon Sep 17 00:00:00 2001 From: magmaus3 Date: Sun, 30 Oct 2022 17:13:01 +0100 Subject: [PATCH] Error page --- iwm_browser/main.py | 26 ++++++++++++++++++++++---- iwm_browser/static/error.css | 11 +++++++++++ iwm_browser/templates/error.html | 26 ++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 4 deletions(-) create mode 100644 iwm_browser/static/error.css create mode 100644 iwm_browser/templates/error.html diff --git a/iwm_browser/main.py b/iwm_browser/main.py index 1d893b6..3e06141 100644 --- a/iwm_browser/main.py +++ b/iwm_browser/main.py @@ -28,6 +28,7 @@ THUMB_URL = "https://images.make.fangam.es" # \S[A-Z0-9]{8} = without dash, no whitespace level_code_regex = re.compile("[A-Z0-9]{4}\-[A-Z0-9]{4}|[A-Z0-9]{8}") +error_template = template_env.get_template("error.html") @app.get("/", response_class=HTMLResponse) async def root(): template = template_env.get_template("home.html") @@ -96,11 +97,16 @@ async def search( "name": search, **last_x_hours, **author }, timeout=10) + if rq.status_code == 503: + return error_template.render(reason="Server is unavailable right now.") searchResults = rq.json() print(rq.url, p * limit) except httpx.ReadTimeout: - return "Server timed out" - + return error_template.render(reason="Server timed out") + except json.decoder.JSONDecodeError: + return error_template.render(reason="Failed to parse server response.", details=rq.text) + except Exception as exc: + return error_template.render(reason="Uncaught exception", details=exc) entryNumber=len(searchResults) @@ -123,10 +129,16 @@ async def showLevel(level_id: int): try: rq = httpx.get(BASE_URL + "/api/v1/map/" + str(level_id), timeout=10) + if rq.status_code == 503: + return error_template.render(reason="Server is unavailable right now") searchResults = rq.json() print(rq.url) except httpx.ReadTimeout: - return "Server timed out" + return error_template.render(reason="Server timed out") + except json.decoder.JSONDecodeError: + return error_template.render(reason="Failed to parse server response.", details=rq.text) + except Exception as exc: + return error_template.render(reason="Uncaught exception", details=exc) @@ -141,10 +153,16 @@ async def showUser(user_id: int): try: rq = httpx.get(BASE_URL + "/api/v1/user/" + str(user_id), timeout=10) + if status_code == 503: + return error_template.render(reason="Server is unavailable right now") searchResults = rq.json() print(rq.url) except httpx.ReadTimeout: - return "Server timed out" + return error_template.render(reason="Server timed out") + except json.decoder.JSONDecodeError: + return error_template.render(reason="Failed to parse server response.", details=rq.text) + except Exception as exc: + return error_template.render(reason="Uncaught exception", details=exc) diff --git a/iwm_browser/static/error.css b/iwm_browser/static/error.css new file mode 100644 index 0000000..1ee29d0 --- /dev/null +++ b/iwm_browser/static/error.css @@ -0,0 +1,11 @@ +body { + font-size: medium; +} + +h1 { + font-size: large; +} + +details * { + font-size: small; +} diff --git a/iwm_browser/templates/error.html b/iwm_browser/templates/error.html new file mode 100644 index 0000000..9776f8c --- /dev/null +++ b/iwm_browser/templates/error.html @@ -0,0 +1,26 @@ + + + + + + + + + + + +
+
+
Something went wrong
+
+

{{ reason }}

+ {% if details %} +
+ More details +
{{ details }}
+
+ {% endif %} +
+
+ +