diff --git a/src/meshcore_hub/web/app.py b/src/meshcore_hub/web/app.py index 417066d..50e069a 100644 --- a/src/meshcore_hub/web/app.py +++ b/src/meshcore_hub/web/app.py @@ -7,8 +7,10 @@ from typing import AsyncGenerator import httpx from fastapi import FastAPI, Request +from fastapi.responses import HTMLResponse from fastapi.staticfiles import StaticFiles from fastapi.templating import Jinja2Templates +from starlette.exceptions import HTTPException as StarletteHTTPException from meshcore_hub import __version__ from meshcore_hub.common.schemas import RadioConfig @@ -150,6 +152,24 @@ def create_app( except Exception as e: return {"status": "not_ready", "api": str(e)} + @app.exception_handler(StarletteHTTPException) + async def http_exception_handler( + request: Request, exc: StarletteHTTPException + ) -> HTMLResponse: + """Handle HTTP exceptions with custom error pages.""" + if exc.status_code == 404: + context = get_network_context(request) + context["request"] = request + context["detail"] = exc.detail if exc.detail != "Not Found" else None + return templates.TemplateResponse( + "errors/404.html", context, status_code=404 + ) + # For other errors, return a simple response + return HTMLResponse( + content=f"

{exc.status_code}

{exc.detail}

", + status_code=exc.status_code, + ) + return app diff --git a/src/meshcore_hub/web/templates/errors/404.html b/src/meshcore_hub/web/templates/errors/404.html new file mode 100644 index 0000000..4e52b78 --- /dev/null +++ b/src/meshcore_hub/web/templates/errors/404.html @@ -0,0 +1,35 @@ +{% extends "base.html" %} + +{% block title %}Page Not Found - {{ network_name }}{% endblock %} + +{% block content %} +
+
+
+
404
+

Page Not Found

+

+ {% if detail %} + {{ detail }} + {% else %} + The page you're looking for doesn't exist or has been moved. + {% endif %} +

+ +
+
+
+{% endblock %}