summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFrédéric Mangano-Tarumi <fmang@mg0.fr>2020-07-28 16:33:27 +0200
committerLukas Fleischer <lfleischer@archlinux.org>2021-02-20 17:24:30 +0100
commit5fb4fc12de1dc374395340724d192271d4aa31f6 (patch)
treea6d7eae153f5a374e3cfec466fec1709da236e62
parent202ffd8923bc3a08bc6d4f18ac6d91441b0b0cfa (diff)
downloadaur-5fb4fc12de1dc374395340724d192271d4aa31f6.tar.gz
aur-5fb4fc12de1dc374395340724d192271d4aa31f6.tar.xz
HTML error pages for FastAPI
Signed-off-by: Lukas Fleischer <lfleischer@archlinux.org>
-rw-r--r--aurweb/asgi.py16
1 files changed, 15 insertions, 1 deletions
diff --git a/aurweb/asgi.py b/aurweb/asgi.py
index 60c7ade7..9293ed77 100644
--- a/aurweb/asgi.py
+++ b/aurweb/asgi.py
@@ -1,4 +1,7 @@
-from fastapi import FastAPI
+import http
+
+from fastapi import FastAPI, HTTPException
+from fastapi.responses import HTMLResponse
from starlette.middleware.sessions import SessionMiddleware
import aurweb.config
@@ -14,3 +17,14 @@ if not session_secret:
app.add_middleware(SessionMiddleware, secret_key=session_secret)
app.include_router(sso.router)
+
+
+@app.exception_handler(HTTPException)
+async def http_exception_handler(request, exc):
+ """
+ Dirty HTML error page to replace the default JSON error responses.
+ In the future this should use a proper Arch-themed HTML template.
+ """
+ phrase = http.HTTPStatus(exc.status_code).phrase
+ return HTMLResponse(f"<h1>{exc.status_code} {phrase}</h1><p>{exc.detail}</p>",
+ status_code=exc.status_code)