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:
Claude
2026-05-23 16:54:55 +09:00
parent 323061df02
commit 73593adb5c
2 changed files with 32 additions and 13 deletions

View File

@@ -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",