{"record":{"id":"a85199bdd6cdc0ed","repo":"Zie619/n8n-workflows","slug":"error-fetching-categories-str-e","errorCode":null,"errorMessage":"Error fetching categories: {str(e)}","messagePattern":"Error fetching categories: (.+?)","errorType":"http","errorClass":"HTTPException","httpStatus":500,"severity":"error","filePath":"api_server.py","lineNumber":661,"sourceCode":"                with open(search_categories_file, \"r\", encoding=\"utf-8\") as f:\n                    search_data = json.load(f)\n\n                unique_categories = set()\n                for item in search_data:\n                    if item.get(\"category\"):\n                        unique_categories.add(item[\"category\"])\n                    else:\n                        unique_categories.add(\"Uncategorized\")\n\n                categories = sorted(list(unique_categories))\n                return {\"categories\": categories}\n            else:\n                # Last resort: return basic categories\n                return {\"categories\": [\"Uncategorized\"]}\n\n    except Exception as e:\n        print(f\"Error loading categories: {e}\")\n        raise HTTPException(\n            status_code=500, detail=f\"Error fetching categories: {str(e)}\"\n        )\n\n\n@app.get(\"/api/category-mappings\")\nasync def get_category_mappings():\n    \"\"\"Get filename to category mappings for client-side filtering.\"\"\"\n    try:\n        search_categories_file = Path(\"context/search_categories.json\")\n        if not search_categories_file.exists():\n            return {\"mappings\": {}}\n\n        with open(search_categories_file, \"r\", encoding=\"utf-8\") as f:\n            search_data = json.load(f)\n\n        # Convert to a simple filename -> category mapping\n        mappings = {}\n        for item in search_data:","sourceCodeStart":643,"sourceCodeEnd":679,"githubUrl":"https://github.com/Zie619/n8n-workflows/blob/94007c1445d9258a7da116646b79473e7c7c3282/api_server.py#L643-L679","documentation":"A generic 500 from /api/categories. The handler tries to load context/unique_categories.json, falls back to building a set from db data (defaulting entries without a 'category' key to 'Uncategorized'), and finally to a hardcoded list. The except still fires when the JSON file exists but is unreadable/corrupt AND the DB fallback path raises, or when any other unexpected error occurs in the chain.","triggerScenarios":"GET /api/categories when context/unique_categories.json exists but contains invalid JSON (truncated generation), when the file lacks read permission, or when the fallback DB query raises (missing table) after the file check fails.","commonSituations":"The context file was partially written by a generator script that was interrupted; the server runs under a different user without read permission on context/; a schema change made the DB fallback query reference a column that no longer exists.","solutions":["Regenerate context/unique_categories.json (re-run the category generation script or the reindex pipeline) so it holds valid JSON.","Validate the file manually: python -c \"import json;json.load(open('context/unique_categories.json'))\" to confirm it parses.","Check the printed server log line 'Error loading categories:' — it contains the underlying exception.","Ensure the process cwd contains the context/ directory, or convert the relative Path to an absolute configured path."],"exampleFix":null,"handlingStrategy":"fallback","validationCode":"import json\nfrom pathlib import Path\n\ndef categories_file_ok(path: str = \"context/unique_categories.json\") -> bool:\n    p = Path(path)\n    if not p.exists():\n        return False\n    try:\n        json.loads(p.read_text(encoding=\"utf-8\"))\n        return True\n    except (json.JSONDecodeError, OSError):\n        return False","typeGuard":null,"tryCatchPattern":"try:\n    cats = client.get(\"/api/categories\").json()[\"categories\"]\nexcept HTTPError as e:\n    if e.response.status_code == 500:\n        cats = [\"Uncategorized\"]  # mirror the server's own last-resort fallback\n    else:\n        raise","preventionTips":["Regenerate context/*.json atomically (write temp file, then os.replace) so readers never see truncated JSON.","Add a startup check that validates every file in context/ parses as JSON.","Treat the categories list as optional UI metadata; default to 'Uncategorized' client-side."],"tags":["http-500","json","file-io","fastapi","fallback"],"backgroundTag":null,"analyzedSha":"94007c1445d9258a7da116646b79473e7c7c3282","analyzedAt":"2026-08-15T04:10:37.591Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}