Skip to content

Commit 2421afe

Browse files
committed
fix(diagnostics): from_dict raises clear error on invalid status (Sentry HIGH)
- DiagnosticStatus(invalid_str) now caught and re-raised with valid options and from_legacy_dict pointer, instead of unhelpful enum error - tests: add invalid status raises with guidance test 81 tests pass.
1 parent 2934b18 commit 2421afe

2 files changed

Lines changed: 19 additions & 1 deletion

File tree

src/qwed_new/core/diagnostics.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,10 +312,20 @@ def from_dict(cls, data: Dict[str, Any]) -> "DiagnosticResult":
312312
ValueError: If agent_message is missing or empty — Layer 1
313313
diagnostics are mandatory and cannot be defaulted
314314
during deserialization.
315+
ValueError: If status is a string not in DiagnosticStatus —
316+
use from_legacy_dict() for pre-#204 engine data.
315317
"""
316318
status = data.get("status", "UNVERIFIABLE")
317319
if isinstance(status, str):
318-
status = DiagnosticStatus(status)
320+
try:
321+
status = DiagnosticStatus(status)
322+
except ValueError:
323+
valid = ", ".join(s.value for s in DiagnosticStatus)
324+
raise ValueError(
325+
f"from_dict: invalid status {status!r} — "
326+
f"must be one of: {valid}. "
327+
"Use from_legacy_dict() for pre-#204 engine data."
328+
) from None
319329

320330
agent_message = data.get("agent_message")
321331
if not agent_message or not str(agent_message).strip():

tests/test_diagnostics.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,14 @@ def test_from_dict_missing_agent_message_raises(self):
434434
DiagnosticResult.from_dict({"status": "UNVERIFIABLE"})
435435
self.assertIn("agent_message", str(ctx.exception))
436436

437+
def test_from_dict_invalid_status_raises_with_guidance(self):
438+
"""Invalid status string must raise with valid options + from_legacy_dict pointer."""
439+
with self.assertRaises(ValueError) as ctx:
440+
DiagnosticResult.from_dict({"status": "CORRECTION_NEEDED", "agent_message": "x"})
441+
msg = str(ctx.exception)
442+
self.assertIn("CORRECTION_NEEDED", msg)
443+
self.assertIn("from_legacy_dict", msg)
444+
437445
def test_from_dict_empty_agent_message_raises(self):
438446
with self.assertRaises(ValueError):
439447
DiagnosticResult.from_dict({"status": "UNVERIFIABLE", "agent_message": " "})

0 commit comments

Comments
 (0)