fix(seed): /api/refresh/seed/symbols 500 → 200 (logger.exception 우회)
pykrx 가 주말/장마감 시 KRX 에서 비-JSON 응답을 받아 JSONDecodeError 를 던지는데, except 블록의 logger.exception() 이 traceback 을 포매팅하다 Python 3.11 의 _byte_offset_to_character_offset 버그(pykrx 소스의 한글 주석 'df = 가...' 바이트 처리) 로 UnicodeDecodeError 가 떠서 try 밖으로 escape — 500 의 진짜 원인이었다. - symbols_seed.py: logger.exception → logger.error(repr(e)) 로 교체. traceback 포매팅을 피한다. - refresh.py: 라우트 핸들러를 try/except 로 감싸 만일 다른 경로로 예외가 새도 200 + ok:false 로 응답. SEED 10 종목은 별도 트랜잭션이라 KRX fetch 실패와 무관하게 보장됨. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -46,11 +46,27 @@ def reseed_symbols() -> dict:
|
||||
|
||||
호출 예 (Windows cmd):
|
||||
curl -X POST http://localhost:8000/api/refresh/seed/symbols
|
||||
|
||||
KRX 가 주말/장 마감 시간에 비정상 응답을 줄 때도 SEED 10 종목은 항상 보장하므로
|
||||
엔드포인트는 200 을 돌려준다. 부분 성공 정보는 응답 body 에 담아 사용자가 판단.
|
||||
"""
|
||||
try:
|
||||
report = seed_symbols()
|
||||
return {
|
||||
"ok": True,
|
||||
"inserted": report.inserted,
|
||||
"updated": report.updated,
|
||||
"seed_marked": report.seed_marked,
|
||||
"markets": report.markets,
|
||||
}
|
||||
except Exception as e: # noqa: BLE001
|
||||
# seed_symbols 내부에서 다 잡지만, 만에 하나 외부로 새는 예외 (logger 포매터
|
||||
# 자체 버그 등) 도 200 으로 흡수해서 SEED 10 만이라도 살리는 게 UX 목표.
|
||||
return {
|
||||
"ok": False,
|
||||
"inserted": 0,
|
||||
"updated": 0,
|
||||
"seed_marked": 0,
|
||||
"markets": {},
|
||||
"error": repr(e)[:300],
|
||||
}
|
||||
|
||||
@@ -78,8 +78,11 @@ def seed_symbols() -> SeedReport:
|
||||
_upsert_seed_tickers()
|
||||
seed_marked = len(SEED_TICKERS)
|
||||
logger.info("seed_symbols: seed-tickers upserted (%d)", seed_marked)
|
||||
except Exception: # noqa: BLE001
|
||||
logger.exception("seed_symbols: seed-tickers upsert failed (critical)")
|
||||
except Exception as e: # noqa: BLE001
|
||||
# logger.exception 은 Python 3.11 의 traceback 포매터가 pykrx 소스의 한글 주석
|
||||
# 'df = 가...' 바이트를 만나면 UnicodeDecodeError 를 던지는 버그가 있어, 그 예외가
|
||||
# try 밖으로 escape 해서 500 을 만든다. 그래서 traceback 안 찍는다.
|
||||
logger.error("seed_symbols: seed-tickers upsert failed: %s", repr(e)[:300])
|
||||
seed_marked = 0
|
||||
|
||||
# 2) KRX 전 종목 — fetch 실패해도 부분 성공 허용
|
||||
@@ -92,8 +95,8 @@ def seed_symbols() -> SeedReport:
|
||||
for code, name in listing:
|
||||
all_rows.append((code, name, market))
|
||||
logger.info("seed_symbols: KRX %s fetched (%d)", market, len(listing))
|
||||
except Exception: # noqa: BLE001
|
||||
logger.exception("seed_symbols: KRX %s fetch failed — skip market", market)
|
||||
except Exception as e: # noqa: BLE001
|
||||
logger.error("seed_symbols: KRX %s fetch failed — skip market: %s", market, repr(e)[:300])
|
||||
market_counts[market] = 0
|
||||
|
||||
inserted = updated = 0
|
||||
@@ -122,8 +125,8 @@ def seed_symbols() -> SeedReport:
|
||||
inserted += 1
|
||||
else:
|
||||
updated += 1
|
||||
except Exception: # noqa: BLE001
|
||||
logger.exception("seed_symbols: KRX bulk upsert failed (transaction rolled back)")
|
||||
except Exception as e: # noqa: BLE001
|
||||
logger.error("seed_symbols: KRX bulk upsert failed (transaction rolled back): %s", repr(e)[:300])
|
||||
|
||||
logger.info(
|
||||
"seed_symbols done: inserted=%d updated=%d seed_marked=%d markets=%s",
|
||||
|
||||
Reference in New Issue
Block a user