🔊 yakalanmamış hata artık LOG BIRAKIYOR (sessiz 503 sınıfı)
Browse files at this commitf3700af9ea84d2923a2b036c65f344c740169f07Parent: 9115967- fix
- 🔊 yakalanmamış hata artık LOG BIRAKIYOR (sessiz 503 sınıfı)
2 changed files +26 -1
- src/service/http_app.py +10 -1
- tests/unit/test_http_hardening.py +16 -0
src/service/http_app.pymodified+10 -1
| @@ -13,6 +13,7 @@ Test: create_app(stub_service) + fastapi.testclient.TestClient (model ge | |||
| 13 | from __future__ import annotations | 13 | from __future__ import annotations |
| 14 | 14 | ||
| 15 | import os | 15 | import os |
| 16 | +import sys | ||
| 16 | import warnings | 17 | import warnings |
| 17 | 18 | ||
| 18 | from fastapi import FastAPI, Header, HTTPException, Request | 19 | from fastapi import FastAPI, Header, HTTPException, Request |
| @@ -302,7 +303,15 @@ def create_app(service, *, service_token: str | None = None, | |||
| 302 | except (asyncio.TimeoutError, TimeoutError): | 303 | except (asyncio.TimeoutError, TimeoutError): |
| 303 | return _tipli_hata(504, "timeout", | 304 | return _tipli_hata(504, "timeout", |
| 304 | "Cevap zamaninda hazir olmadi, tekrar dener misin?", rid) | 305 | "Cevap zamaninda hazir olmadi, tekrar dener misin?", rid) |
| 305 | - except Exception: | 306 | + except Exception as e: # noqa: BLE001 |
| 307 | + # SESSİZ 503 SINIFI (canlıda yakalandı, 2026-09-17): bu dal | ||
| 308 | + # yakalanmamış hatayı tipli cevaba çevirir ama İZ BIRAKMIYORDU — | ||
| 309 | + # iki `/rag/chat` isteği 503 döndü ve logda nedeni HİÇ görünmedi; | ||
| 310 | + # gerçek sebep (sağlayıcının geçici hatası) ancak elle tekrar | ||
| 311 | + # deneyip gözlemleyerek anlaşıldı. Artık tür + mesaj (kısaltılmış) | ||
| 312 | + # loglanır; SIR yazılmaz, istemciye de yalnız tipli gövde gider. | ||
| 313 | + print(f"[http][HATA] istek {rid}: {type(e).__name__}: {str(e)[:300]}", | ||
| 314 | + file=sys.stderr, flush=True) | ||
| 306 | return _tipli_hata(503, "service_unavailable", | 315 | return _tipli_hata(503, "service_unavailable", |
| 307 | "Şu anda cevap üretemiyorum, biraz sonra tekrar dene.", rid) | 316 | "Şu anda cevap üretemiyorum, biraz sonra tekrar dene.", rid) |
| 308 | resp.headers["X-Request-Id"] = rid | 317 | resp.headers["X-Request-Id"] = rid |
tests/unit/test_http_hardening.pymodified+16 -0
| @@ -228,6 +228,22 @@ class ErrorContractTests(unittest.TestCase): | |||
| 228 | self.assertTrue(body["text"]) # kullanıcıya Türkçe mesaj | 228 | self.assertTrue(body["text"]) # kullanıcıya Türkçe mesaj |
| 229 | self.assertIn("request_id", body) | 229 | self.assertIn("request_id", body) |
| 230 | 230 | ||
| 231 | + def test_unhandled_error_is_also_logged_with_its_cause(self): | ||
| 232 | + """SESSİZ 503: tipli gövde tek başına yetmez — operatör nedeni logdan | ||
| 233 | + görebilmeli (canlıda iki 503 logda HİÇ iz bırakmadı).""" | ||
| 234 | + import contextlib | ||
| 235 | + import io | ||
| 236 | + err = io.StringIO() | ||
| 237 | + c = TestClient(create_app(_Exploding(), rate_limit_per_min=0), | ||
| 238 | + raise_server_exceptions=False) | ||
| 239 | + with contextlib.redirect_stderr(err): | ||
| 240 | + r = c.post("/rag/chat", json={"query": "q"}) | ||
| 241 | + self.assertEqual(r.status_code, 503) | ||
| 242 | + iz = err.getvalue() | ||
| 243 | + self.assertIn("[http][HATA]", iz) | ||
| 244 | + self.assertIn("RuntimeError", iz) # hata TÜRÜ logda | ||
| 245 | + self.assertIn("429", iz) # nedeni taşıyan mesaj logda | ||
| 246 | + | ||
| 231 | def test_request_id_header_on_success(self): | 247 | def test_request_id_header_on_success(self): |
| 232 | r = _client().post("/rag/chat", json={"query": "q"}) | 248 | r = _client().post("/rag/chat", json={"query": "q"}) |
| 233 | self.assertTrue(r.headers.get("X-Request-Id")) | 249 | self.assertTrue(r.headers.get("X-Request-Id")) |