fix: hide readiness exception details (#113)
This commit is contained in:
parent
2891a0cfd8
commit
6be016d658
14
src/main.py
14
src/main.py
|
|
@ -22,6 +22,10 @@ FRONTEND_DIR = Path(__file__).resolve().parent.parent / "frontend"
|
||||||
class ContextPayloadError(ValueError):
|
class ContextPayloadError(ValueError):
|
||||||
"""Raised when Gitea returns a structurally invalid context payload."""
|
"""Raised when Gitea returns a structurally invalid context payload."""
|
||||||
|
|
||||||
|
|
||||||
|
class ReadinessPayloadError(ValueError):
|
||||||
|
"""Raised when Gitea returns a structurally invalid readiness payload."""
|
||||||
|
|
||||||
app.add_middleware(
|
app.add_middleware(
|
||||||
CORSMiddleware,
|
CORSMiddleware,
|
||||||
allow_origins=["*"],
|
allow_origins=["*"],
|
||||||
|
|
@ -56,13 +60,19 @@ async def readiness():
|
||||||
current_user(), timeout=READINESS_TIMEOUT_SECONDS
|
current_user(), timeout=READINESS_TIMEOUT_SECONDS
|
||||||
)
|
)
|
||||||
if not isinstance(user, dict) or not user.get("login"):
|
if not isinstance(user, dict) or not user.get("login"):
|
||||||
raise ValueError("Gitea current-user response did not include a login")
|
raise ReadinessPayloadError(
|
||||||
|
"Gitea current-user response did not include a login"
|
||||||
|
)
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
timed_out = isinstance(exc, TimeoutError)
|
timed_out = isinstance(exc, TimeoutError)
|
||||||
error_message = (
|
error_message = (
|
||||||
f"Gitea readiness check timed out after {READINESS_TIMEOUT_SECONDS:g}s"
|
f"Gitea readiness check timed out after {READINESS_TIMEOUT_SECONDS:g}s"
|
||||||
if timed_out
|
if timed_out
|
||||||
else str(exc)
|
else (
|
||||||
|
str(exc)
|
||||||
|
if isinstance(exc, ReadinessPayloadError)
|
||||||
|
else "Gitea readiness check is temporarily unavailable"
|
||||||
|
)
|
||||||
)
|
)
|
||||||
return JSONResponse(
|
return JSONResponse(
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -29,17 +29,21 @@ async def test_readiness_endpoint_reports_connected_gitea_user(monkeypatch):
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.anyio
|
@pytest.mark.anyio
|
||||||
async def test_readiness_endpoint_returns_503_when_gitea_is_unavailable(monkeypatch):
|
async def test_readiness_endpoint_hides_unexpected_upstream_error_details(monkeypatch):
|
||||||
async def unavailable_user():
|
async def unavailable_user():
|
||||||
raise RuntimeError("connection refused")
|
raise RuntimeError("secret internal upstream detail")
|
||||||
|
|
||||||
monkeypatch.setattr(main, "current_user", unavailable_user)
|
monkeypatch.setattr(main, "current_user", unavailable_user)
|
||||||
|
|
||||||
response = await main.readiness()
|
response = await main.readiness()
|
||||||
|
|
||||||
assert response.status_code == 503
|
assert response.status_code == 503
|
||||||
assert b'"status":"not_ready"' in response.body
|
assert json.loads(response.body) == {
|
||||||
assert b'"error":"connection refused"' in response.body
|
"status": "not_ready",
|
||||||
|
"service": "stackchain-dashboard",
|
||||||
|
"error": "Gitea readiness check is temporarily unavailable",
|
||||||
|
}
|
||||||
|
assert b"secret internal upstream detail" not in response.body
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.anyio
|
@pytest.mark.anyio
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user