{
  "name": "05 - Branching, Error Handling and Debugging",
  "nodes": [
    {
      "parameters": {
        "content": "## 05 Branching, Errors, Debugging\nTry these in Sample Input:\n- statusToSimulate = 200  -> API OK\n- statusToSimulate = 500  -> retries twice, then Handled Error\n- remove the key from chatInput -> Missing Key\n\nDebug tip: pin Sample Input output (pin icon) to replay the same data.\nError Trigger: copy it to its own workflow and set it as Error Workflow in Settings.",
        "height": 280,
        "width": 520
      },
      "id": "05000000-0000-4000-8000-000000000000",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [-60, -340]
    },
    {
      "parameters": {},
      "id": "05000000-0000-4000-8000-000000000001",
      "name": "Start",
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [0, 0]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            { "id": "05a00000-0000-4000-8000-000000000001", "name": "chatInput", "value": "create a Jira ticket with VWO-123", "type": "string" },
            { "id": "05a00000-0000-4000-8000-000000000002", "name": "statusToSimulate", "value": 500, "type": "number" }
          ]
        },
        "options": {}
      },
      "id": "05000000-0000-4000-8000-000000000002",
      "name": "Sample Input",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [200, 0]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "05b00000-0000-4000-8000-000000000001",
              "name": "issueKey",
              "value": "={{ ($json.chatInput.toUpperCase().match(/[A-Z][A-Z0-9]+-\\d+/) || [''])[0] }}",
              "type": "string"
            }
          ]
        },
        "includeOtherFields": true,
        "options": {}
      },
      "id": "05000000-0000-4000-8000-000000000003",
      "name": "Extract Key",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [400, 0]
    },
    {
      "parameters": {
        "conditions": {
          "options": { "caseSensitive": true, "leftValue": "", "typeValidation": "loose", "version": 2 },
          "conditions": [
            {
              "id": "05c00000-0000-4000-8000-000000000001",
              "leftValue": "={{ $json.issueKey }}",
              "rightValue": "",
              "operator": { "type": "string", "operation": "notEmpty", "singleValue": true }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "05000000-0000-4000-8000-000000000004",
      "name": "Has Key?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [600, 0]
    },
    {
      "parameters": {
        "url": "=https://httpbin.org/status/{{ $json.statusToSimulate }}",
        "options": {}
      },
      "id": "05000000-0000-4000-8000-000000000005",
      "name": "Call API (may fail)",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [820, -100],
      "retryOnFail": true,
      "maxTries": 2,
      "waitBetweenTries": 1000,
      "onError": "continueErrorOutput"
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            { "id": "05d00000-0000-4000-8000-000000000001", "name": "result", "value": "=API OK for {{ $('Extract Key').item.json.issueKey }}", "type": "string" }
          ]
        },
        "options": {}
      },
      "id": "05000000-0000-4000-8000-000000000006",
      "name": "API OK",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [1060, -200]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            { "id": "05e00000-0000-4000-8000-000000000001", "name": "result", "value": "Handled Error", "type": "string" },
            { "id": "05e00000-0000-4000-8000-000000000002", "name": "reason", "value": "={{ $json.error ? $json.error.message : 'unknown error' }}", "type": "string" }
          ]
        },
        "options": {}
      },
      "id": "05000000-0000-4000-8000-000000000007",
      "name": "Handled Error",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [1060, 0]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            { "id": "05f00000-0000-4000-8000-000000000001", "name": "result", "value": "Missing Key: please include one like VWO-123", "type": "string" }
          ]
        },
        "options": {}
      },
      "id": "05000000-0000-4000-8000-000000000008",
      "name": "Missing Key",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [820, 150]
    },
    {
      "parameters": {},
      "id": "05000000-0000-4000-8000-000000000009",
      "name": "Error Trigger",
      "type": "n8n-nodes-base.errorTrigger",
      "typeVersion": 1,
      "position": [0, 360]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "05g00000-0000-4000-8000-000000000001",
              "name": "alert",
              "value": "=Workflow {{ $json.workflow.name }} failed at node {{ $json.execution.lastNodeExecuted }}: {{ $json.execution.error.message }}",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "id": "05000000-0000-4000-8000-000000000010",
      "name": "Alert Team",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [220, 360]
    }
  ],
  "connections": {
    "Start": { "main": [[{ "node": "Sample Input", "type": "main", "index": 0 }]] },
    "Sample Input": { "main": [[{ "node": "Extract Key", "type": "main", "index": 0 }]] },
    "Extract Key": { "main": [[{ "node": "Has Key?", "type": "main", "index": 0 }]] },
    "Has Key?": {
      "main": [
        [{ "node": "Call API (may fail)", "type": "main", "index": 0 }],
        [{ "node": "Missing Key", "type": "main", "index": 0 }]
      ]
    },
    "Call API (may fail)": {
      "main": [
        [{ "node": "API OK", "type": "main", "index": 0 }],
        [{ "node": "Handled Error", "type": "main", "index": 0 }]
      ]
    },
    "Error Trigger": { "main": [[{ "node": "Alert Team", "type": "main", "index": 0 }]] }
  },
  "pinData": {},
  "settings": { "executionOrder": "v1" },
  "active": false
}
