docs: 계획서 Task 4 예시 except 튜플의 예외 누출 수정
리뷰에서 잡힌 구멍이 계획서 참고 코드에도 그대로 있었다. str.format 의 ValueError 와 .decode 의 UnicodeDecodeError(= ValueError 서브클래스)가 빠져 있어, 그대로 옮기면 "어떤 실패에도 예외 없음" 계약이 깨진다. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
ee30e87bb4
commit
88768020bf
@ -502,8 +502,8 @@ def ai_pick_cuts(cuts, comments, quotas, *, model: str = AI_MODEL,
|
|||||||
try:
|
try:
|
||||||
prompt = prompts.load_recommend().format(
|
prompt = prompts.load_recommend().format(
|
||||||
cuts=_cut_lines(cuts), comments=_candidate_lines(comments), k=k)
|
cuts=_cut_lines(cuts), comments=_candidate_lines(comments), k=k)
|
||||||
except (KeyError, IndexError, OSError): # 사용자가 프롬프트를 깨뜨린 경우
|
except (KeyError, IndexError, ValueError, OSError): # 사용자가 프롬프트를 깨뜨린 경우
|
||||||
return {}
|
return {} # ValueError: str.format 은 중괄호가 깨지면 이걸 던진다
|
||||||
body = {
|
body = {
|
||||||
"contents": [{"parts": [{"text": prompt}]}],
|
"contents": [{"parts": [{"text": prompt}]}],
|
||||||
"generationConfig": {"temperature": 0.3,
|
"generationConfig": {"temperature": 0.3,
|
||||||
@ -518,8 +518,9 @@ def ai_pick_cuts(cuts, comments, quotas, *, model: str = AI_MODEL,
|
|||||||
with urllib.request.urlopen(req, timeout=timeout) as resp:
|
with urllib.request.urlopen(req, timeout=timeout) as resp:
|
||||||
data = json.loads(resp.read().decode("utf-8"))
|
data = json.loads(resp.read().decode("utf-8"))
|
||||||
raw = data["candidates"][0]["content"]["parts"][0]["text"]
|
raw = data["candidates"][0]["content"]["parts"][0]["text"]
|
||||||
except (urllib.error.URLError, KeyError, IndexError, TimeoutError,
|
# URLError·TimeoutError 는 OSError 서브클래스라 따로 안 적는다.
|
||||||
json.JSONDecodeError, OSError):
|
# ValueError 는 .decode("utf-8") 의 UnicodeDecodeError 를 덮는다(OSError 가 아니다).
|
||||||
|
except (KeyError, IndexError, json.JSONDecodeError, ValueError, OSError):
|
||||||
return {} # 429 포함 — 폴백은 호출부 몫
|
return {} # 429 포함 — 폴백은 호출부 몫
|
||||||
return _parse_ai(raw)
|
return _parse_ai(raw)
|
||||||
```
|
```
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user