diff --git a/src/service/http_app.py b/src/service/http_app.py index 8b73da6..6f34e17 100644 --- a/src/service/http_app.py +++ b/src/service/http_app.py @@ -13,6 +13,7 @@ Test: create_app(stub_service) + fastapi.testclient.TestClient (model ge from __future__ import annotations import os +import sys import warnings from fastapi import FastAPI, Header, HTTPException, Request @@ -302,7 +303,15 @@ def create_app(service, *, service_token: str | None = None, except (asyncio.TimeoutError, TimeoutError): return _tipli_hata(504, "timeout", "Cevap zamaninda hazir olmadi, tekrar dener misin?", rid) - except Exception: + except Exception as e: # noqa: BLE001 + # SESSİZ 503 SINIFI (canlıda yakalandı, 2026-09-17): bu dal + # yakalanmamış hatayı tipli cevaba çevirir ama İZ BIRAKMIYORDU — + # iki `/rag/chat` isteği 503 döndü ve logda nedeni HİÇ görünmedi; + # gerçek sebep (sağlayıcının geçici hatası) ancak elle tekrar + # deneyip gözlemleyerek anlaşıldı. Artık tür + mesaj (kısaltılmış) + # loglanır; SIR yazılmaz, istemciye de yalnız tipli gövde gider. + print(f"[http][HATA] istek {rid}: {type(e).__name__}: {str(e)[:300]}", + file=sys.stderr, flush=True) return _tipli_hata(503, "service_unavailable", "Şu anda cevap üretemiyorum, biraz sonra tekrar dene.", rid) resp.headers["X-Request-Id"] = rid diff --git a/tests/unit/test_http_hardening.py b/tests/unit/test_http_hardening.py index f67b852..445e3ac 100644 --- a/tests/unit/test_http_hardening.py +++ b/tests/unit/test_http_hardening.py @@ -228,6 +228,22 @@ class ErrorContractTests(unittest.TestCase): self.assertTrue(body["text"]) # kullanıcıya Türkçe mesaj self.assertIn("request_id", body) + def test_unhandled_error_is_also_logged_with_its_cause(self): + """SESSİZ 503: tipli gövde tek başına yetmez — operatör nedeni logdan + görebilmeli (canlıda iki 503 logda HİÇ iz bırakmadı).""" + import contextlib + import io + err = io.StringIO() + c = TestClient(create_app(_Exploding(), rate_limit_per_min=0), + raise_server_exceptions=False) + with contextlib.redirect_stderr(err): + r = c.post("/rag/chat", json={"query": "q"}) + self.assertEqual(r.status_code, 503) + iz = err.getvalue() + self.assertIn("[http][HATA]", iz) + self.assertIn("RuntimeError", iz) # hata TÜRÜ logda + self.assertIn("429", iz) # nedeni taşıyan mesaj logda + def test_request_id_header_on_success(self): r = _client().post("/rag/chat", json={"query": "q"}) self.assertTrue(r.headers.get("X-Request-Id"))