diff --git a/README.md b/README.md index fa8129e..ee4086c 100644 --- a/README.md +++ b/README.md @@ -78,7 +78,7 @@ Pin a tag, not a branch: ```toml dependencies = [ - "chrome @ git+https://git.wierzbowa.cloud/michal/chrome@v0.5.0", + "chrome @ git+https://git.wierzbowa.cloud/michal/chrome@v0.6.0", ] ``` diff --git a/chrome/__init__.py b/chrome/__init__.py index f5c7755..ca786b0 100644 --- a/chrome/__init__.py +++ b/chrome/__init__.py @@ -7,7 +7,7 @@ need an `if service == "marketplace"`, it belongs in the service. from pathlib import Path -__version__ = "0.5.0" +__version__ = "0.6.0" PACKAGE_DIR = Path(__file__).parent STATIC_DIR = PACKAGE_DIR / "static" diff --git a/chrome/app.py b/chrome/app.py index 577f6b8..e17cf82 100644 --- a/chrome/app.py +++ b/chrome/app.py @@ -25,6 +25,19 @@ from starlette.exceptions import HTTPException as StarletteHTTPException from . import STATIC_DIR, TEMPLATES_DIR, WEBAWESOME_VERSION, assets, i18n +def _error_body(detail: object) -> dict: + """A JSON error carrying both conventional keys. + + chrome started with `error`, and marketplace and agent-bridge read that. + FastAPI's own convention is `detail`, and scribe's client was written + against it long before chrome existed - `(await r.json()).detail || msg`, + which degrades every error message in its UI to "Bad Request" if the key + is missing. Sending both costs a few bytes and spares two codebases a + rewrite over a naming difference. + """ + return {"error": detail, "detail": detail} + + def _safe_local(target: str | None) -> str | None: """``target`` if it is a local path, else None. @@ -77,6 +90,7 @@ class Chrome: mount_prefix: str = "", static_path: str = "/static", language_switcher: bool = True, + health: bool = True, ) -> None: self.app = app self.service = service @@ -132,7 +146,11 @@ class Chrome: #: object taken out of it. Takes a Request, returns a dict. self.page_context = None - app.include_router(health_router(service, version)) + # A service with a richer probe of its own keeps it - scribe's + # reports which speech engines it can reach, which is the thing + # worth knowing when it is unwell. + if health: + app.include_router(health_router(service, version)) self._install_language_route() self._install_error_pages() @@ -225,7 +243,7 @@ class Chrome: if request.url.path.startswith(("/api/", "/healthz")): from fastapi.responses import JSONResponse - return JSONResponse({"error": exc.detail}, status_code=exc.status_code) + return JSONResponse(_error_body(exc.detail), status_code=exc.status_code) key = "404" if exc.status_code == 404 else "500" locale = self.translations.locale_for(request) return self.render( diff --git a/pyproject.toml b/pyproject.toml index ddf0a0a..f957511 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "chrome" -version = "0.5.0" +version = "0.6.0" description = "Shared page shell, design tokens and service helpers for the internal services." requires-python = ">=3.13" dependencies = [ diff --git a/tests/test_chrome.py b/tests/test_chrome.py index 6c7ae79..ae16c66 100644 --- a/tests/test_chrome.py +++ b/tests/test_chrome.py @@ -80,10 +80,15 @@ def test_html_errors_render_the_themed_page(client): assert "error-code" in r.text and "Test Service" in r.text -def test_api_errors_stay_json(client): +def test_api_errors_stay_json_under_both_conventional_keys(client): + """`error` is what chrome started with and what marketplace and + agent-bridge read; `detail` is FastAPI's own, which scribe's client was + written against years earlier. Sending both spares a rewrite over a + naming difference - and a client reading the missing one degrades + silently to the HTTP status text, which is the worst way to find out.""" r = client.get("/api/boom") assert r.status_code == 404 - assert r.json() == {"error": "no such thing"} + assert r.json() == {"error": "no such thing", "detail": "no such thing"} def test_language_cookie_round_trips(client): @@ -373,3 +378,17 @@ def test_a_service_can_supply_its_own_language_control(tmp_path): assert "data-lang-select" not in without # Only the language control stands down; the theme toggle is unaffected. assert "data-theme-choice" in without + + +def test_a_service_can_keep_its_own_health_probe(tmp_path): + """scribe's reports which speech engines it can reach, which is the + thing worth knowing when it is unwell. chrome's would otherwise win by + being registered first.""" + app = FastAPI() + Chrome(app, service="t", version="1", title="T", health=False) + + @app.get("/healthz") + def healthz(): + return {"ok": True, "engine": "reachable"} + + assert TestClient(app).get("/healthz").json() == {"ok": True, "engine": "reachable"}