Skip to content

Commit ede08b8

Browse files
committed
fix: P2 issue 19 error handling for corrupted JSON registry
Signed-off-by: habeck <habeck@us.ibm.com>
1 parent ce866f7 commit ede08b8

1 file changed

Lines changed: 20 additions & 2 deletions

File tree

cpex/tools/plugin_registry.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,26 @@ def __init__(self, *args, **kwargs):
3232
DEFAULT_PLUGIN_REGISTRY_FILE = "installed-plugins.json"
3333
ipr_file = DEFAULT_PLUGIN_REGISTRY_FOLDER / DEFAULT_PLUGIN_REGISTRY_FILE
3434
if ipr_file.exists():
35-
with open(ipr_file, "r", encoding="utf-8") as ipr:
36-
self.registry = InstalledPluginRegistry(**json.load(ipr))
35+
try:
36+
with open(ipr_file, "r", encoding="utf-8") as ipr:
37+
self.registry = InstalledPluginRegistry(**json.load(ipr))
38+
except (json.JSONDecodeError, ValueError, KeyError) as e:
39+
# If registry is corrupted, log error and start fresh
40+
import logging
41+
logger = logging.getLogger(__name__)
42+
logger.error(
43+
"Corrupted plugin registry file at %s: %s. Starting with empty registry.",
44+
ipr_file,
45+
str(e)
46+
)
47+
# Backup the corrupted file
48+
backup_file = ipr_file.with_suffix(".json.corrupted")
49+
try:
50+
ipr_file.rename(backup_file)
51+
logger.info("Backed up corrupted registry to %s", backup_file)
52+
except Exception as backup_error:
53+
logger.warning("Could not backup corrupted registry: %s", str(backup_error))
54+
self.registry = InstalledPluginRegistry()
3755
else:
3856
self.registry = InstalledPluginRegistry()
3957

0 commit comments

Comments
 (0)