{
  "name": "Log n8n workflow failures to Google Sheets and alert on Telegram (data intake sync companion)",
  "nodes": [
    {
      "parameters": {
        "content": "## Companion error handler for the data intake sync workflow\n\nCatches failures that per-node error outputs cannot: timeouts, crashes between nodes, and the attachment or AI extraction steps, which stop the run on purpose rather than write bad data. Each failure becomes one row in a Google Sheets \"Error Log\" tab and one Telegram alert with the failing node, the error, a next step and the execution link.\n\n### How it works\n1. The Error Trigger fires when the workflow it is assigned to fails.\n2. A Set node builds a record with the same columns as the main audit log.\n3. The row is appended to the Error Log tab.\n4. A Telegram alert is posted. If the sheet write fails, a second Telegram node still alerts and says the row was not written.\n\n### Setup\n1. Import this workflow and connect Google Sheets and Telegram credentials.\n2. Add an \"Error Log\" tab with the audit columns plus execution_url, and pick the spreadsheet in Append Error Row.\n3. Set the Telegram chat ID in both alert nodes.\n4. Open the main workflow, go to Settings > Error Workflow and select this workflow.",
        "height": 760,
        "width": 680
      },
      "id": "cba34431-0758-47f4-bea1-3ebfaad9f086",
      "name": "Overview",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -820,
        -200
      ]
    },
    {
      "parameters": {
        "content": "## Need this built and run for you?\nWant this set up, adapted to your stack and monitored for your business? [signalnine.io](https://signalnine.io/?utm_source=n8n-template&utm_medium=template&utm_campaign=data-intake-sync)\n\nHow this template works, and what has and has not been tested: [signalnine.io/systems/data-intake-sync](https://signalnine.io/systems/data-intake-sync?utm_source=n8n-template&utm_medium=template&utm_campaign=data-intake-sync)",
        "height": 240,
        "width": 680,
        "color": 6
      },
      "id": "a504eff9-78d1-42b9-aaf4-d41f23966b7e",
      "name": "Need help",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -820,
        600
      ]
    },
    {
      "parameters": {
        "content": "## Record and alert\nBuild one error record, append it to the Error Log tab, and alert on Telegram even if the sheet write fails.",
        "height": 540,
        "width": 880,
        "color": 7
      },
      "id": "942cafd3-6407-4065-881d-1b4c39d1877e",
      "name": "Section error",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -60,
        -160
      ]
    },
    {
      "name": "Catch Workflow Error",
      "type": "n8n-nodes-base.errorTrigger",
      "typeVersion": 1,
      "position": [
        0,
        0
      ],
      "parameters": {},
      "id": "49597977-386d-44c5-9852-d9c8b8799d36"
    },
    {
      "name": "Build Error Record",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        220,
        0
      ],
      "parameters": {
        "mode": "manual",
        "includeOtherFields": false,
        "assignments": {
          "assignments": [
            {
              "id": "e-a",
              "name": "template",
              "value": "reliable-data-intake-sync",
              "type": "string"
            },
            {
              "id": "e-b",
              "name": "correlation_id",
              "value": "={{ \"rdi_run_\" + String($json.execution?.id ?? \"unknown\") }}",
              "type": "string"
            },
            {
              "id": "e-c",
              "name": "status",
              "value": "handled_failure",
              "type": "string"
            },
            {
              "id": "e-d",
              "name": "stage",
              "value": "={{ String($json.execution?.lastNodeExecuted ?? \"unknown\") }}",
              "type": "string"
            },
            {
              "id": "e-e",
              "name": "summary",
              "value": "={{ \"Workflow-level failure in \" + String($json.workflow?.name ?? \"\") + \" (execution \" + String($json.execution?.id ?? \"\") + \")\" }}",
              "type": "string"
            },
            {
              "id": "e-f",
              "name": "entity_type",
              "value": "run",
              "type": "string"
            },
            {
              "id": "e-g",
              "name": "entity_id",
              "value": "={{ String($json.execution?.id ?? \"\") }}",
              "type": "string"
            },
            {
              "id": "e-h",
              "name": "error_code",
              "value": "={{ String($json.execution?.error?.name ?? \"error\") }}",
              "type": "string"
            },
            {
              "id": "e-i",
              "name": "error_message",
              "value": "={{ String($json.execution?.error?.message ?? \"unknown error\").slice(0, 500) }}",
              "type": "string"
            },
            {
              "id": "e-j",
              "name": "error_node",
              "value": "={{ String($json.execution?.error?.node?.name ?? $json.execution?.lastNodeExecuted ?? \"\") }}",
              "type": "string"
            },
            {
              "id": "e-k",
              "name": "execution_url",
              "value": "={{ String($json.execution?.url ?? \"\") }}",
              "type": "string"
            },
            {
              "id": "e-l",
              "name": "timestamp",
              "value": "={{ $now.toISO() }}",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "id": "8bf650ef-991a-4b2f-a4cc-37c4db7f4cd9"
    },
    {
      "name": "Append Error Row",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.7,
      "position": [
        440,
        0
      ],
      "parameters": {
        "resource": "sheet",
        "operation": "append",
        "documentId": {
          "__rl": true,
          "mode": "id",
          "value": "",
          "cachedResultName": ""
        },
        "sheetName": {
          "__rl": true,
          "mode": "name",
          "value": "Error Log",
          "cachedResultName": "Error Log"
        },
        "columns": {
          "mappingMode": "autoMapInputData",
          "value": {},
          "matchingColumns": [],
          "schema": [
            {
              "id": "template",
              "displayName": "template",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "correlation_id",
              "displayName": "correlation_id",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "status",
              "displayName": "status",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "stage",
              "displayName": "stage",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "summary",
              "displayName": "summary",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "entity_type",
              "displayName": "entity_type",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "entity_id",
              "displayName": "entity_id",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "error_code",
              "displayName": "error_code",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "error_message",
              "displayName": "error_message",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "error_node",
              "displayName": "error_node",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "execution_url",
              "displayName": "execution_url",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "timestamp",
              "displayName": "timestamp",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            }
          ]
        },
        "options": {
          "cellFormat": "RAW"
        }
      },
      "retryOnFail": true,
      "maxTries": 3,
      "waitBetweenTries": 5000,
      "onError": "continueErrorOutput",
      "id": "5111fb1f-21a1-4f35-b8d2-393380059c72"
    },
    {
      "name": "Post Failure Alert",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        660,
        0
      ],
      "parameters": {
        "resource": "message",
        "operation": "sendMessage",
        "chatId": "",
        "text": "={{ (() => { const h = (v) => String(v ?? '').replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;'); const E = $('Build Error Record').item.json; const W = $('Catch Workflow Error').item.json; return ['❌ RDI · Workflow failed at ' + h(E.error_node || E.stage), 'Workflow: ' + h(W.workflow?.name ?? 'Reliable Data Intake Sync'), 'Error: ' + h(E.error_message), 'Fix the input, then re-run execution ' + h(E.entity_id), '#rdi · run ' + h(E.correlation_id) + ' · ' + h(E.execution_url)].join('\\n'); })() }}",
        "additionalFields": {
          "appendAttribution": false,
          "disable_web_page_preview": true,
          "parse_mode": "HTML"
        }
      },
      "notes": "Alert for failures that escape per-node handling. Convention: line 1 ❌ RDI · Workflow failed at <node>, the failing workflow name from the Error Trigger payload, the error, a next-action line, last line #rdi · run rdi_run_<executionId> · <execution url> (the same string as the Error Log row). Telegram here; swap for Slack/Email if the client alerts elsewhere.",
      "retryOnFail": true,
      "maxTries": 3,
      "waitBetweenTries": 5000,
      "id": "49504631-bc11-4173-9e93-b698d8950a2d"
    },
    {
      "name": "Post Failure Alert (Sheet Down)",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        660,
        220
      ],
      "parameters": {
        "resource": "message",
        "operation": "sendMessage",
        "chatId": "",
        "text": "={{ (() => { const h = (v) => String(v ?? '').replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;'); const E = $('Build Error Record').item.json; const W = $('Catch Workflow Error').item.json; return ['❌ RDI · Workflow failed at ' + h(E.error_node || E.stage) + ' — Error Log unreachable', 'Workflow: ' + h(W.workflow?.name ?? 'Reliable Data Intake Sync'), 'Error: ' + h(E.error_message), '⚠️ Error Log append failed: ' + h($json.error?.message ?? $json.error), 'Restore the Error Log sheet, then add the row for ' + h(E.correlation_id) + ' by hand', '#rdi · run ' + h(E.correlation_id) + ' · ' + h(E.execution_url)].join('\\n'); })() }}",
        "additionalFields": {
          "appendAttribution": false,
          "disable_web_page_preview": true,
          "parse_mode": "HTML"
        }
      },
      "notes": "Same alert, taken when the Error Log append itself failed, so a broken sheet never swallows the failure; the headline says the Error Log is unreachable and the next action is to add the row by hand.",
      "retryOnFail": true,
      "maxTries": 3,
      "waitBetweenTries": 5000,
      "id": "157b810e-e78c-4369-afd2-7866913242d8"
    }
  ],
  "connections": {
    "Catch Workflow Error": {
      "main": [
        [
          {
            "node": "Build Error Record",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Build Error Record": {
      "main": [
        [
          {
            "node": "Append Error Row",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Append Error Row": {
      "main": [
        [
          {
            "node": "Post Failure Alert",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Post Failure Alert (Sheet Down)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "pinData": {}
}
