diff --git a/aurweb/asgi.py b/aurweb/asgi.py index b399cfb1..ef8d5933 100644 --- a/aurweb/asgi.py +++ b/aurweb/asgi.py @@ -6,8 +6,8 @@ import typing from urllib.parse import quote_plus -from fastapi import FastAPI, HTTPException, Request -from fastapi.responses import HTMLResponse, RedirectResponse +from fastapi import FastAPI, HTTPException, Request, Response +from fastapi.responses import RedirectResponse from fastapi.staticfiles import StaticFiles from prometheus_client import multiprocess from sqlalchemy import and_, or_ @@ -21,10 +21,11 @@ from aurweb.auth import BasicAuthBackend from aurweb.db import get_engine, query from aurweb.models import AcceptedTerm, Term from aurweb.prometheus import http_api_requests_total, http_requests_total, instrumentator -from aurweb.routers import accounts, auth, errors, html, packages, rpc, rss, sso, trusted_user +from aurweb.routers import accounts, auth, html, packages, rpc, rss, sso, trusted_user +from aurweb.templates import make_context, render_template # Setup the FastAPI app. -app = FastAPI(exception_handlers=errors.exceptions) +app = FastAPI() # Instrument routes with the prometheus-fastapi-instrumentator # library with custom collectors and expose /metrics. @@ -93,14 +94,15 @@ def child_exit(server, worker): # pragma: no cover @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. - """ +async def http_exception_handler(request: Request, exc: HTTPException) \ + -> Response: + """ Handle an HTTPException thrown in a route. """ phrase = http.HTTPStatus(exc.status_code).phrase - return HTMLResponse(f"
{exc.detail}
", - status_code=exc.status_code) + context = make_context(request, phrase) + context["exc"] = exc + context["phrase"] = phrase + return render_template(request, "errors/detail.html", context, + exc.status_code) @app.middleware("http") diff --git a/aurweb/routers/errors.py b/aurweb/routers/errors.py deleted file mode 100644 index 9ed1e80d..00000000 --- a/aurweb/routers/errors.py +++ /dev/null @@ -1,21 +0,0 @@ -from http import HTTPStatus - -from aurweb.templates import make_context, render_template - - -async def not_found(request, exc): - context = make_context(request, "Page Not Found") - return render_template(request, "errors/404.html", context, - HTTPStatus.NOT_FOUND) - - -async def service_unavailable(request, exc): - context = make_context(request, "Service Unavailable") - return render_template(request, "errors/503.html", context, - HTTPStatus.SERVICE_UNAVAILABLE) - -# Maps HTTP errors to functions -exceptions = { - 404: not_found, - 503: service_unavailable -} diff --git a/templates/errors/404.html b/templates/errors/404.html deleted file mode 100644 index 4926aff6..00000000 --- a/templates/errors/404.html +++ /dev/null @@ -1,8 +0,0 @@ -{% extends 'partials/layout.html' %} - -{% block pageContent %} -{% trans %}Sorry, the page you've requested does not exist.{% endtrans %}
-{% trans %}Don't panic! This site is down due to maintenance. We will be back soon.{% endtrans %}
-{{ exc.detail }}
+{exc.detail}
" + response = await aurweb.asgi.http_exception_handler(Request(), exc) + assert response.status_code == 422 + content = response.body.decode() + assert f"{exc.status_code} - {phrase}" in content + assert "EXCEPTION!" in content @pytest.mark.asyncio