-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparse_warnings.py
More file actions
74 lines (64 loc) · 2.5 KB
/
Copy pathparse_warnings.py
File metadata and controls
74 lines (64 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
from __future__ import annotations
from dataclasses import dataclass
@dataclass
class ParseWarningCollector:
"""Accumulates parse failures skipped during bubble/composer processing."""
composers_skipped: int = 0
bubbles_skipped: int = 0
composers_processing_failed: int = 0
def record_composer_skipped(self, count: int = 1) -> None:
if count > 0:
self.composers_skipped += count
def record_bubble_skipped(self, count: int = 1) -> None:
if count > 0:
self.bubbles_skipped += count
def record_composer_processing_failure(self, count: int = 1) -> None:
"""Post-parse assembly failed; not a JSON/schema parse skip."""
if count > 0:
self.composers_processing_failed += count
@property
def has_warnings(self) -> bool:
return (
self.composers_skipped > 0
or self.bubbles_skipped > 0
or self.composers_processing_failed > 0
)
def to_api_list(self) -> list[dict]:
"""Structured warnings for JSON API responses (issue #67)."""
warnings: list[dict] = []
if self.composers_skipped:
n = self.composers_skipped
noun = "conversation" if n == 1 else "conversations"
warnings.append({
"type": "parse_error",
"count": n,
"detail": (
f"{n} {noun} could not be loaded due to schema or JSON parse errors"
),
})
if self.bubbles_skipped:
n = self.bubbles_skipped
noun = "message" if n == 1 else "messages"
warnings.append({
"type": "parse_error",
"count": n,
"detail": (
f"{n} {noun} could not be loaded due to schema or JSON parse errors"
),
})
if self.composers_processing_failed:
n = self.composers_processing_failed
noun = "conversation" if n == 1 else "conversations"
warnings.append({
"type": "processing_error",
"count": n,
"detail": (
f"{n} {noun} could not be fully assembled after parsing"
),
})
return warnings
def attach_to(self, payload: dict) -> dict:
"""Add ``warnings`` to a dict response when any failures were recorded."""
if self.has_warnings:
payload = {**payload, "warnings": self.to_api_list()}
return payload