mirror of
https://github.com/unshackle-dl/unshackle.git
synced 2026-11-22 00:45:10 +00:00
feat(api): tag each error with a ref and count errors by service
Every structured error carries a short ref, appended to its message and logged with the request; /api/dashboard/status gains per-service error counts. Gzip now compresses in place.
This commit is contained in:
@@ -61,4 +61,4 @@ def test_compresses_large_json_when_accepted() -> None:
|
||||
assert resp.headers.get("Content-Encoding") == "gzip"
|
||||
decompressed = gzip.decompress(resp.body)
|
||||
assert json.loads(decompressed) == big
|
||||
assert resp.headers["Content-Length"] == str(len(resp.body))
|
||||
assert resp.content_length == len(resp.body)
|
||||
|
||||
@@ -73,7 +73,7 @@ def test_build_error_response_from_api_error() -> None:
|
||||
body = _body(resp)
|
||||
assert body["status"] == "error"
|
||||
assert body["error_code"] == "SESSION_NOT_FOUND"
|
||||
assert body["message"] == "no such session"
|
||||
assert body["message"] == f"no such session (ref {body['ref']})"
|
||||
assert body["details"] == {"session_id": "abc"}
|
||||
assert "retryable" not in body
|
||||
assert "debug_info" not in body
|
||||
@@ -91,7 +91,7 @@ def test_build_error_response_from_generic_exception() -> None:
|
||||
assert resp.status == 500
|
||||
body = _body(resp)
|
||||
assert body["error_code"] == "INTERNAL_ERROR"
|
||||
assert body["message"] == "oops"
|
||||
assert body["message"] == f"oops (ref {body['ref']})"
|
||||
|
||||
|
||||
def test_build_error_response_debug_mode_includes_traceback() -> None:
|
||||
@@ -188,6 +188,6 @@ def test_error_contract_apierror_passthrough_full_shape() -> None:
|
||||
assert resp.status == 429
|
||||
body = _body(resp)
|
||||
assert body["error_code"] == "RATE_LIMITED"
|
||||
assert body["message"] == "slow down"
|
||||
assert body["message"] == f"slow down (ref {body['ref']})"
|
||||
assert body["details"] == {"retry_after": 5}
|
||||
assert body["retryable"] is True
|
||||
|
||||
@@ -9,6 +9,7 @@ from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import json
|
||||
from collections import Counter, defaultdict
|
||||
|
||||
import pytest
|
||||
from aiohttp import web
|
||||
@@ -132,7 +133,7 @@ async def test_api_handler_maps_apierror(aiohttp_client) -> None:
|
||||
body = await resp.json()
|
||||
assert body["status"] == "error"
|
||||
assert body["error_code"] == "NOT_FOUND"
|
||||
assert body["message"] == "nope"
|
||||
assert body["message"] == f"nope (ref {body['ref']})"
|
||||
assert body["details"] == {"x": 1}
|
||||
|
||||
|
||||
@@ -180,6 +181,7 @@ async def test_heartbeat_late_error_counts_as_rejected(aiohttp_client, monkeypat
|
||||
|
||||
monkeypatch.setattr(routes, "HEARTBEAT_INTERVAL", 0.05)
|
||||
monkeypatch.setattr(stats, "keys", {})
|
||||
monkeypatch.setattr(stats, "errors", defaultdict(Counter))
|
||||
|
||||
@heartbeat
|
||||
async def slow_fail(request: web.Request) -> web.Response:
|
||||
@@ -195,6 +197,33 @@ async def test_heartbeat_late_error_counts_as_rejected(aiohttp_client, monkeypat
|
||||
sent = len(await resp.read())
|
||||
(entry,) = stats.keys.values()
|
||||
assert resp.status == 200 and entry.rejected == 1 and entry.bytes_out >= sent > 0
|
||||
assert stats.errors == {"-": {"SERVICE_ERROR": 1}}
|
||||
|
||||
|
||||
@pytest.mark.parametrize(("service", "counted_as"), [("EXAMPLE", "EXAMPLE"), ("NOT_INSTALLED", "-")])
|
||||
async def test_error_is_counted_by_service_and_logged_with_its_ref(
|
||||
aiohttp_client, monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture, service: str, counted_as: str
|
||||
) -> None:
|
||||
"""A gzipped error still reaches the stats: counted under an installed service only, and logged with its ref."""
|
||||
from unshackle.core.api.stats import stats, stats_middleware
|
||||
|
||||
monkeypatch.setattr(stats, "errors", defaultdict(Counter))
|
||||
|
||||
@api_handler
|
||||
async def fail(request: web.Request) -> web.Response:
|
||||
await request.json()
|
||||
raise APIError(APIErrorCode.SERVICE_ERROR, "denied", details={"pad": "x" * 300})
|
||||
|
||||
app = web.Application(middlewares=[stats_middleware, compression_middleware])
|
||||
app["debug_api"] = False
|
||||
app.router.add_post("/fail", fail)
|
||||
client = await aiohttp_client(app)
|
||||
|
||||
resp = await client.post("/fail", json={"service": service}, headers={"Accept-Encoding": "gzip"})
|
||||
body = await resp.json()
|
||||
assert resp.headers["Content-Encoding"] == "gzip"
|
||||
assert stats.errors == {counted_as: {"SERVICE_ERROR": 1}}
|
||||
assert f"Error ref {body['ref']}: SERVICE_ERROR 422 POST /fail service={counted_as}" in caplog.text
|
||||
|
||||
|
||||
async def test_heartbeat_client_disconnect_is_quiet(
|
||||
|
||||
Reference in New Issue
Block a user