{"record":{"id":"d28adf41f65a38d5","repo":"Zie619/n8n-workflows","slug":"error-generating-diagram-str-e","errorCode":null,"errorMessage":"Error generating diagram: {str(e)}","messagePattern":"Error generating diagram: (.+?)","errorType":"http","errorClass":"HTTPException","httpStatus":500,"severity":"error","filePath":"api_server.py","lineNumber":497,"sourceCode":"            data = json.load(f)\n\n        nodes = data.get(\"nodes\", [])\n        connections = data.get(\"connections\", {})\n\n        # Generate Mermaid diagram\n        diagram = generate_mermaid_diagram(nodes, connections)\n\n        return {\"diagram\": diagram}\n    except HTTPException:\n        raise\n    except json.JSONDecodeError as e:\n        print(f\"Error parsing JSON in {filename}: {str(e)}\")\n        raise HTTPException(\n            status_code=400, detail=f\"Invalid JSON in workflow file: {str(e)}\"\n        )\n    except Exception as e:\n        print(f\"Error generating diagram for {filename}: {str(e)}\")\n        raise HTTPException(\n            status_code=500, detail=f\"Error generating diagram: {str(e)}\"\n        )\n\n\ndef generate_mermaid_diagram(nodes: List[Dict], connections: Dict) -> str:\n    \"\"\"Generate Mermaid.js flowchart code from workflow nodes and connections.\"\"\"\n    if not nodes:\n        return \"graph TD\\n  EmptyWorkflow[No nodes found in workflow]\"\n\n    # Create mapping for node names to ensure valid mermaid IDs\n    mermaid_ids = {}\n    for i, node in enumerate(nodes):\n        node_id = f\"node{i}\"\n        node_name = node.get(\"name\", f\"Node {i}\")\n        mermaid_ids[node_name] = node_id\n\n    # Start building the mermaid diagram\n    mermaid_code = [\"graph TD\"]","sourceCodeStart":479,"sourceCodeEnd":515,"githubUrl":"https://github.com/Zie619/n8n-workflows/blob/94007c1445d9258a7da116646b79473e7c7c3282/api_server.py#L479-L515","documentation":"The generic 500 from GET /api/workflows/{filename}/diagram, reached only after HTTPException and JSONDecodeError branches are excluded. It means the file parsed successfully but generate_mermaid_diagram(nodes, connections) — or the surrounding code — threw while converting nodes/connections into Mermaid flowchart text. The exception is logged ('Error generating diagram for {filename}') and wrapped into the 500 detail.","triggerScenarios":"A workflow whose node objects lack the fields generate_mermaid_diagram() assumes (missing 'name'/'type'), connection keys referencing nonexistent nodes, non-string node names breaking Mermaid ID mapping, or unexpected node shapes (n8n version differences) tripping the ID-sanitization logic.","commonSituations":"Indexing workflows exported by a newer/older n8n with extra nesting; workflows containing sticky notes or special nodes with unusual schemas; node names with quotes/unicode that escape the ID mapping.","solutions":["Read the detail text — it carries the exact exception from generate_mermaid_diagram (e.g. KeyError: 'name').","Inspect the workflow JSON's nodes/connections arrays for the shape the generator expects (list of dicts with name/type; connections keyed by node name).","Make generate_mermaid_diagram defensive: use node.get('name', f'node_{i}') and skip malformed entries instead of throwing.","As a stopgap, view the raw workflow JSON via the detail endpoint while the diagram bug is fixed."],"exampleFix":"# before\nfor i, node in enumerate(nodes):\n    mermaid_ids[node['name']] = f'N{i}'\n\n# after (tolerate missing/odd fields)\nfor i, node in enumerate(nodes):\n    name = node.get('name') or f'node_{i}'\n    mermaid_ids[name] = f'N{i}'","handlingStrategy":"try-catch","validationCode":"def has_expected_shape(path):\n    import json\n    data = json.load(open(path, encoding='utf-8'))\n    nodes = data.get('nodes', [])\n    conns = data.get('connections', {})\n    return (\n        isinstance(nodes, list)\n        and all(isinstance(n, dict) for n in nodes)\n        and isinstance(conns, dict)\n        and all(isinstance(k, str) for k in conns)\n    )","typeGuard":null,"tryCatchPattern":"try:\n    d = client.get(f'/api/workflows/{name}/diagram').json()['diagram']\nexcept HTTPError as e:\n    if e.response.status_code == 500:\n        d = 'graph TD\\n  Error[Diagram generation failed — see raw JSON view]'","preventionTips":["Expect node schemas to vary across n8n versions; render diagrams with a graceful fallback.","Report the embedded exception detail upstream — it identifies the exact field the generator choked on.","Prefer .get() with defaults in diagram generation code for optional node fields."],"tags":["fastapi","diagram","mermaid","http-500","data-shape"],"backgroundTag":null,"analyzedSha":"94007c1445d9258a7da116646b79473e7c7c3282","analyzedAt":"2026-08-15T04:10:37.591Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}