Skip to content

Commit d998458

Browse files
committed
error page
1 parent 4f79d6b commit d998458

2 files changed

Lines changed: 25 additions & 4 deletions

File tree

app/core/exceptions.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,31 @@
1+
from pathlib import Path
2+
13
from fastapi import FastAPI, Request
24
from fastapi.responses import HTMLResponse, JSONResponse
35
from starlette.exceptions import HTTPException as StarletteHTTPException
46

57
from app.core.templates import render_template
68

9+
FRONTEND_INDEX = Path(__file__).resolve().parents[2] / "frontend" / "dist" / "index.html"
10+
SPA_REDIRECT_HTML = """<!doctype html>
11+
<html lang="en">
12+
<head>
13+
<meta charset="utf-8" />
14+
<title>Redirecting…</title>
15+
<meta http-equiv="refresh" content="0; url=/app/404" />
16+
<script>
17+
window.location.replace("/app/404");
18+
</script>
19+
</head>
20+
<body style="font-family: sans-serif; background: #05070f; color: #f8fafc; text-align: center; padding: 3rem;">
21+
<p>Routing you to the AlterBase CDN app…</p>
22+
<noscript>
23+
JavaScript is required for the dashboard. <a href="/app/404">Continue to the Not Found page</a>.
24+
</noscript>
25+
</body>
26+
</html>
27+
"""
28+
729

830
def register_exception_handlers(app: FastAPI) -> None:
931
@app.exception_handler(404)
@@ -12,6 +34,9 @@ async def not_found_handler(request: Request, exc: StarletteHTTPException):
1234
detail = exc.detail if hasattr(exc, "detail") else "Not Found"
1335
if "application/json" in accept and "text/html" not in accept:
1436
return JSONResponse({"detail": detail}, status_code=404)
37+
if FRONTEND_INDEX.exists() and ("text/html" in accept or "application/json" not in accept):
38+
# Surface the React SPA's NotFound route while keeping a 404 status code
39+
return HTMLResponse(content=SPA_REDIRECT_HTML, status_code=404)
1540
detail_text = (
1641
detail
1742
if detail not in (None, "", "Not found", "Not Found")

tests/test_app.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,6 @@ def _prepare_client(tmp_path, monkeypatch, *, rate_limit="5", max_size=str(10 *
2323
monkeypatch.setenv("CACHE_MAX_AGE_SECONDS", cache_age)
2424
monkeypatch.setenv("ADMIN_PASSWORD", "test-admin")
2525
monkeypatch.setenv("ADMIN_LOCK_STEP_SECONDS", lock_step)
26-
monkeypatch.setenv("MEGA_BACKUP_ENABLED", "false")
27-
monkeypatch.setenv("MEGA_EMAIL", "")
28-
monkeypatch.setenv("MEGA_PASSWORD", "")
29-
monkeypatch.setenv("MEGA_FOLDER_NAME", "")
3026

3127
# Reload modules so configuration changes take effect cleanly.
3228
module_order = [

0 commit comments

Comments
 (0)