{"record":{"id":"7872aa536f1bc08f","repo":"abi/screenshot-to-code","slug":"design-systems-storage-is-not-valid-json","errorCode":null,"errorMessage":"Design systems storage is not valid JSON","messagePattern":"Design systems storage is not valid JSON","errorType":"http","errorClass":"HTTPException","httpStatus":500,"severity":"error","filePath":"backend/routes/design_systems.py","lineNumber":73,"sourceCode":"            id=str(raw_item[\"id\"]),\n            name=str(raw_item[\"name\"]),\n            content=str(raw_item.get(\"content\", \"\")),\n            createdAt=str(raw_item[\"createdAt\"]),\n            updatedAt=str(raw_item[\"updatedAt\"]),\n        )\n    except KeyError:\n        return None\n\n\ndef read_design_systems() -> list[DesignSystem]:\n    file_path = get_design_systems_file_path()\n    if not file_path.exists():\n        return []\n\n    try:\n        raw_items = cast(list[Any], json.loads(file_path.read_text(encoding=\"utf-8\")))\n    except json.JSONDecodeError as exc:\n        raise HTTPException(\n            status_code=500,\n            detail=\"Design systems storage is not valid JSON\",\n        ) from exc\n\n    if not isinstance(raw_items, list):\n        raise HTTPException(\n            status_code=500,\n            detail=\"Design systems storage must contain a list\",\n        )\n\n    design_systems: list[DesignSystem] = []\n    for raw_item in raw_items:\n        design_system = parse_design_system(raw_item)\n        if design_system:\n            design_systems.append(design_system)\n    return design_systems\n\n","sourceCodeStart":55,"sourceCodeEnd":91,"githubUrl":"https://github.com/abi/screenshot-to-code/blob/d026163f586dfa8c5c10d28c36edd59a9d3b0e88/backend/routes/design_systems.py#L55-L91","documentation":"Raised by read_design_systems() (500) when design-systems.json — stored under ~/.screenshot-to-code (or SCREENSHOT_TO_CODE_DATA_DIR) — exists but json.loads() fails. The file is user-writable storage, so manual edits, partial writes, or merge conflicts can corrupt it. Every design-systems endpoint fails with this until the file is fixed, because reads happen before any operation.","triggerScenarios":"Any GET/POST/PUT/DELETE on /api/design-systems while the JSON file is malformed: trailing commas from hand edits, a half-written file after a crash mid-write, concatenated objects from concurrent writers.","commonSituations":"Editing design-systems.json by hand and leaving invalid JSON; two backend processes writing simultaneously (the writer is not locked); sync tools (Dropbox etc.) producing conflict files that get merged badly.","solutions":["Validate and repair the file: python -m json.tool ~/.screenshot-to-code/design-systems.json to locate the syntax error.","Restore from a backup or rewrite as '[]' to start clean (data loss of stored systems).","Avoid hand-editing; use the API/UI for all changes.","Ensure only one backend instance writes to the same data dir."],"exampleFix":"# before: design-systems.json contains {\"items\": [ ... },  (trailing comma)\nGET /api/design-systems  # 500 'not valid JSON'\n\n# after\ncat ~/.screenshot-to-code/design-systems.json | python -m json.tool   # find error\n# fix or reset:\necho '[]' > ~/.screenshot-to-code/design-systems.json","handlingStrategy":"validation","validationCode":"import json, pathlib\n\ndef design_systems_file_is_valid(path: pathlib.Path) -> bool:\n    if not path.exists():\n        return True  # absent file is fine\n    try:\n        json.loads(path.read_text(encoding=\"utf-8\"))\n        return True\n    except json.JSONDecodeError:\n        return False","typeGuard":"def is_parsable_design_store(raw: str) -> bool:\n    try:\n        json.loads(raw)\n        return True\n    except json.JSONDecodeError:\n        return False","tryCatchPattern":"try:\n    systems = requests.get(url + \"/api/design-systems\").raise_for_status().json()\nexcept requests.HTTPError as e:\n    if e.response is not None and e.response.status_code == 500 and \"valid JSON\" in e.response.text:\n        raise RuntimeError(\"design-systems.json corrupted — repair or reset the file\") from e\n    raise","preventionTips":["Run python -m json.tool on design-systems.json after any manual edit.","Keep backups before hand-editing the file.","Ensure a single backend process writes the file (no concurrent writers)."],"tags":["fastapi","http-500","json","corruption","design-systems","storage"],"backgroundTag":null,"analyzedSha":"d026163f586dfa8c5c10d28c36edd59a9d3b0e88","analyzedAt":"2026-08-14T22:02:06.951Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}