44
55import json
66import logging
7- from copy import copy
87from dataclasses import dataclass
98from typing import Any
109
@@ -27,18 +26,17 @@ def _dot_pop(data: dict, key: str) -> str | None: # type: ignore[type-arg]
2726 return None
2827
2928
30- def _pop_key (data : dict , keys : list [str ], fallback : str ) -> Any : # type: ignore[type-arg]
31- """Return result of recursively popping each key while searching for a match."""
32- try :
33- key = keys .pop (0 )
34- except IndexError :
29+ def _pop_key (data : dict , keys : list [str ], index : int , fallback : str ) -> Any : # type: ignore[type-arg]
30+ """Return result of recursively searching for a matching key."""
31+ if index >= len (keys ):
3532 return fallback
36- return _dot_pop (data , key ) or _pop_key (data , keys , fallback )
33+ key = keys [index ]
34+ return _dot_pop (data , key ) or _pop_key (data , keys , index + 1 , fallback )
3735
3836
3937def pop_key (data : dict , keys : list [str ], fallback : str ) -> Any : # type: ignore[type-arg]
4038 """Return the first key in the data or default to the fallback."""
41- return _pop_key (data , copy ( keys ) , fallback )
39+ return _pop_key (data , keys , 0 , fallback )
4240
4341
4442@dataclass
@@ -65,7 +63,8 @@ def print_record(line: str, console: Console, config: Config) -> None:
6563 """Format and print the record."""
6664 try :
6765 record = Record .from_line (json .loads (line ), config = config )
68- except Exception :
66+ except (json .JSONDecodeError , ValueError , KeyError , TypeError , AttributeError ):
67+ # Handle JSON parsing errors, missing keys, type errors, and attribute access issues
6968 console .print (line .rstrip (), markup = False , highlight = False ) # Print the unmodified line
7069 return
7170
0 commit comments