{"record":{"id":"8f56cc487446a042","repo":"Zie619/n8n-workflows","slug":"error-loading-workflow-str-e","errorCode":null,"errorMessage":"Error loading workflow: {str(e)}","messagePattern":"Error loading workflow: (.+?)","errorType":"http","errorClass":"HTTPException","httpStatus":500,"severity":"error","filePath":"api_server.py","lineNumber":367,"sourceCode":"                            f\"Security: Blocked access to file outside workflows: {target_file}\"\n                        )\n                        continue\n\n        if not matching_file:\n            print(f\"Warning: File {filename} not found in workflows directory\")\n            raise HTTPException(\n                status_code=404,\n                detail=f\"Workflow file '{filename}' not found on filesystem\",\n            )\n\n        with open(matching_file, \"r\", encoding=\"utf-8\") as f:\n            raw_json = json.load(f)\n\n        return {\"metadata\": workflow_meta, \"raw_json\": raw_json}\n    except HTTPException:\n        raise\n    except Exception as e:\n        raise HTTPException(status_code=500, detail=f\"Error loading workflow: {str(e)}\")\n\n\n@app.get(\"/api/workflows/{filename}/download\")\nasync def download_workflow(filename: str, request: Request):\n    \"\"\"Download workflow JSON file with security validation.\"\"\"\n    try:\n        # Security: Validate filename to prevent path traversal\n        if not validate_filename(filename):\n            print(f\"Security: Blocked path traversal attempt for filename: {filename}\")\n            raise HTTPException(status_code=400, detail=\"Invalid filename format\")\n\n        # Security: Rate limiting\n        client_ip = request.client.host if request.client else \"unknown\"\n        if not check_rate_limit(client_ip):\n            raise HTTPException(\n                status_code=429, detail=\"Rate limit exceeded. Please try again later.\"\n            )\n","sourceCodeStart":349,"sourceCodeEnd":385,"githubUrl":"https://github.com/Zie619/n8n-workflows/blob/94007c1445d9258a7da116646b79473e7c7c3282/api_server.py#L349-L385","documentation":"The catch-all 500 for GET /api/workflows/{filename}: any exception that is not an HTTPException raised while opening and json.load()-ing the matched file is converted to HTTPException(500, 'Error loading workflow: ...'). HTTPExceptions raised earlier in the handler are re-raised untouched (except HTTPException: raise), so this message specifically means an unexpected filesystem or JSON error.","triggerScenarios":"open() failing with PermissionError/IsADirectoryError after the existence check raced a deletion; json.load() raising JSONDecodeError on a truncated or BOM-prefixed file; a decode error because the file is not UTF-8 despite being opened with encoding='utf-8'.","commonSituations":"Concurrently editing workflow JSONs while the server reads them; Windows-encoded or corrupted files committed to the repo; permission changes on the workflows tree under a service account.","solutions":["Read the detail text: it embeds the original exception (e.g. 'Expecting value: line 1 column 1'), which pinpoints parse vs permission problems.","Validate the file: python -c \"import json;json.load(open('workflows/<sub>/<file>.json',encoding='utf-8'))\".","Fix permissions or ownership so the server process can read the file.","Re-save non-UTF-8 files as UTF-8 and ensure files are written atomically (temp file + rename) to avoid truncated reads."],"exampleFix":"# before\nwith open(matching_file, 'r', encoding='utf-8') as f:\n    raw_json = json.load(f)\n\n# after (explicit friendly errors for the two real failure modes)\ntry:\n    with open(matching_file, 'r', encoding='utf-8') as f:\n        raw_json = json.load(f)\nexcept json.JSONDecodeError as e:\n    raise HTTPException(status_code=400, detail=f'Invalid JSON in workflow file: {str(e)}')\nexcept OSError as e:\n    raise HTTPException(status_code=500, detail=f'Cannot read workflow file: {str(e)}')","handlingStrategy":"try-catch","validationCode":"import json, pathlib\n\ndef readable_valid_json(path):\n    p = pathlib.Path(path)\n    if not p.is_file():\n        return False\n    try:\n        json.loads(p.read_text(encoding='utf-8'))\n        return True\n    except (json.JSONDecodeError, UnicodeDecodeError, OSError):\n        return False","typeGuard":null,"tryCatchPattern":"try:\n    data = client.get(f'/api/workflows/{name}').json()\nexcept HTTPError as e:\n    detail = e.response.json().get('detail', '')\n    if 'Expecting value' in detail or 'Extra data' in detail:\n        flag_file_as_corrupt(name)   # JSON problem\n    else:\n        flag_file_as_unreadable(name)  # permission/OS problem","preventionTips":["Lint every workflow JSON in CI (json.load) before merge.","Write workflow files atomically (temp + rename).","Keep the server process's read permissions on workflows/ stable."],"tags":["fastapi","filesystem","json","http-500"],"backgroundTag":null,"analyzedSha":"94007c1445d9258a7da116646b79473e7c7c3282","analyzedAt":"2026-08-15T04:10:37.591Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}