Commit ddb470c
fix(parser): surface TypeError as MessageParseError on malformed payloads
parse_message documents that malformed messages raise MessageParseError, but
each case-branch's try/except only catches KeyError. When a field is present
but of the wrong type (e.g. rate_limit_event with rate_limit_info=None, or
user/assistant with message=None), the subsequent indexing raises TypeError,
which escapes the parser and crashes the read loop -- losing every subsequent
message in the stream.
Add a TypeError clause alongside the existing KeyError clause in the six case
branches that index sub-fields. Existing "Missing required field..." wording
is preserved for KeyError (backward-compatible with existing tests); the new
clause emits "Malformed field..." through the same MessageParseError type and
carries the original payload.
Co-Authored-By: Claude <noreply@anthropic.com>1 parent 9970096 commit ddb470c
2 files changed
Lines changed: 61 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
122 | 122 | | |
123 | 123 | | |
124 | 124 | | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
125 | 129 | | |
126 | 130 | | |
127 | 131 | | |
| |||
184 | 188 | | |
185 | 189 | | |
186 | 190 | | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
187 | 195 | | |
188 | 196 | | |
189 | 197 | | |
| |||
242 | 250 | | |
243 | 251 | | |
244 | 252 | | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
245 | 257 | | |
246 | 258 | | |
247 | 259 | | |
| |||
275 | 287 | | |
276 | 288 | | |
277 | 289 | | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
278 | 294 | | |
279 | 295 | | |
280 | 296 | | |
| |||
288 | 304 | | |
289 | 305 | | |
290 | 306 | | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
291 | 311 | | |
292 | 312 | | |
293 | 313 | | |
| |||
310 | 330 | | |
311 | 331 | | |
312 | 332 | | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
313 | 337 | | |
314 | 338 | | |
315 | 339 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
754 | 754 | | |
755 | 755 | | |
756 | 756 | | |
| 757 | + | |
| 758 | + | |
| 759 | + | |
| 760 | + | |
| 761 | + | |
| 762 | + | |
| 763 | + | |
| 764 | + | |
| 765 | + | |
| 766 | + | |
| 767 | + | |
| 768 | + | |
| 769 | + | |
| 770 | + | |
| 771 | + | |
| 772 | + | |
| 773 | + | |
| 774 | + | |
| 775 | + | |
| 776 | + | |
| 777 | + | |
| 778 | + | |
| 779 | + | |
| 780 | + | |
| 781 | + | |
| 782 | + | |
| 783 | + | |
| 784 | + | |
| 785 | + | |
| 786 | + | |
| 787 | + | |
| 788 | + | |
| 789 | + | |
| 790 | + | |
| 791 | + | |
| 792 | + | |
| 793 | + | |
757 | 794 | | |
758 | 795 | | |
759 | 796 | | |
| |||
0 commit comments