{"record":{"id":"2a5f8a7b200ceb9b","repo":"Zie619/n8n-workflows","slug":"error-fetching-category-mappings-str-e","errorCode":null,"errorMessage":"Error fetching category mappings: {str(e)}","messagePattern":"Error fetching category mappings: (.+?)","errorType":"http","errorClass":"HTTPException","httpStatus":500,"severity":"error","filePath":"api_server.py","lineNumber":689,"sourceCode":"        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:\n            filename = item.get(\"filename\")\n            category = item.get(\"category\") or \"Uncategorized\"\n            if filename:\n                mappings[filename] = category\n\n        return {\"mappings\": mappings}\n\n    except Exception as e:\n        print(f\"Error loading category mappings: {e}\")\n        raise HTTPException(\n            status_code=500, detail=f\"Error fetching category mappings: {str(e)}\"\n        )\n\n\n@app.get(\"/api/workflows/category/{category}\", response_model=SearchResponse)\nasync def search_workflows_by_category(\n    category: str,\n    page: int = Query(1, ge=1, description=\"Page number\"),\n    per_page: int = Query(20, ge=1, le=100, description=\"Items per page\"),\n):\n    \"\"\"Search workflows by service category (messaging, database, ai_ml, etc.).\"\"\"\n    try:\n        offset = (page - 1) * per_page\n\n        workflows, total = db.search_by_category(\n            category=category, limit=per_page, offset=offset\n        )\n","sourceCodeStart":671,"sourceCodeEnd":707,"githubUrl":"https://github.com/Zie619/n8n-workflows/blob/94007c1445d9258a7da116646b79473e7c7c3282/api_server.py#L671-L707","documentation":"A generic 500 from /api/category-mappings. The handler reads context/search_categories.json (returning empty mappings if the file is absent) and builds a filename->category dict; any exception during file read, JSON parse, or iteration over malformed items is re-raised as HTTP 500 with the original error text.","triggerScenarios":"GET /api/category-mappings when context/search_categories.json exists but is corrupt JSON, when an item in the loaded array is not a dict (so .get raises AttributeError), or when the file cannot be opened due to permissions.","commonSituations":"search_categories.json truncated by a crashed generator; file written with a BOM or wrong encoding so json.load fails; items occasionally being strings instead of objects after a producer change.","solutions":["Regenerate context/search_categories.json by re-running the search-category build/reindex step.","Sanity-check the file: python -c \"import json;d=json.load(open('context/search_categories.json'));print(type(d), len(d))\" — it must be a list of objects.","Read the server log line 'Error loading category mappings:' for the concrete exception, then fix that specific cause (permissions, encoding, structure).","If mappings are non-critical for your client, tolerate an empty result by deleting/renaming the broken file — the handler then returns {\"mappings\": {}} cleanly."],"exampleFix":null,"handlingStrategy":"fallback","validationCode":"import json\nfrom pathlib import Path\n\ndef mappings_source_ok(path: str = \"context/search_categories.json\") -> bool:\n    p = Path(path)\n    if not p.exists():\n        return True  # absent file is a supported case (empty mappings)\n    try:\n        data = json.loads(p.read_text(encoding=\"utf-8\"))\n        return isinstance(data, list) and all(isinstance(i, dict) for i in data)\n    except (json.JSONDecodeError, OSError):\n        return False","typeGuard":null,"tryCatchPattern":"try:\n    mappings = client.get(\"/api/category-mappings\").json()[\"mappings\"]\nexcept HTTPError as e:\n    if e.response.status_code == 500:\n        mappings = {}  # client-side filtering still works, just without categories\n    else:\n        raise","preventionTips":["Validate generated JSON files in CI (parse + schema check) before deploy.","Make the producer write atomically to avoid readers seeing partial files.","Cache mappings client-side so a broken file does not take down filtering UI."],"tags":["http-500","json","file-io","fastapi","data-mapping"],"backgroundTag":null,"analyzedSha":"94007c1445d9258a7da116646b79473e7c7c3282","analyzedAt":"2026-08-15T04:10:37.591Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}