Skip to content

Commit

Permalink
catch all json config errors
Browse files Browse the repository at this point in the history
  • Loading branch information
ariffjeff committed Apr 22, 2024
1 parent 0dca9dc commit 0d59d4b
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions houdini_package_manager/wrangle/config_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -654,14 +654,17 @@ def extract_data(self) -> None:
def _load(self) -> None:
"""
Load json contents.
Handles invalid json such as directory paths with single \\ character delimiters.
Handles invalid json. If the json cannot be recovered, an empty dict will be set.
"""

if not isinstance(self.config_path, Path):
raise TypeError("path must be a pathlib.Path object.")

# convert invalid json values that are paths with '\' to '\\' to prevent json loading error
class JSONPathDecoder(json.JSONDecoder):
"""
Tries to parse invalid json into valid json by accounting for some possible errors.
"""

def decode(self, s, **kwargs):
regex_replacements = [
(re.compile(r"([^\\])\\([^\\])"), r"\1\\\\\2"), # Fix single backslashes in paths
Expand All @@ -678,8 +681,11 @@ def decode(self, s, **kwargs):
except json.decoder.JSONDecodeError:
logging.warning(f"Invalid json (might fail to resolve/parse): {self.config_path}")
self.warnings.append("Invalid JSON! Fix errors and refresh this table.")
with open(self.config_path) as f:
data = json.load(f, cls=JSONPathDecoder)
try:
with open(self.config_path) as f:
data = json.load(f, cls=JSONPathDecoder)
except Exception: # final catch all for all other broken json errors
data = {}

self._raw_json = data
self.config = data
Expand Down

0 comments on commit 0d59d4b

Please sign in to comment.