{
  "name": "Log n8n workflow failures to Google Sheets and alert on Telegram (lead qualification companion)",
  "nodes": [
    {
      "parameters": {
        "content": "## Companion error handler for the lead qualification workflow\n\nCatches failures that per-node error outputs cannot: timeouts, crashes between nodes and errors in nodes without an error branch. Each failure becomes one row in a Google Sheets \"Error Log\" tab and one Telegram alert with the failing node, the error message, a suggested next step and a link to the execution.\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 itself 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 columns template, correlation_id, status, stage, summary, entity_type, entity_id, error_code, error_message, error_node, execution_url, timestamp, 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": "ec293833-4acd-4240-8b7e-aec903719947",
      "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=lead-to-reply)\n\nHow this template works, and what has and has not been tested: [signalnine.io/systems/lead-to-reply](https://signalnine.io/systems/lead-to-reply?utm_source=n8n-template&utm_medium=template&utm_campaign=lead-to-reply)",
        "height": 240,
        "width": 680,
        "color": 6
      },
      "id": "5efcfac8-ac73-48a4-b013-aa1f6dad2a50",
      "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": "dd9177fb-9e39-491d-9a06-3da2391f2731",
      "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": "35846fcf-0b4f-467a-b6a3-64241fe3d63e"
    },
    {
      "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": "revenue-lifecycle-engine",
              "type": "string"
            },
            {
              "id": "e-b",
              "name": "correlation_id",
              "value": "={{ \"rle_wf_error_\" + 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": "record",
              "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": "493026a4-09c5-4b9f-b84e-e2471d13ef4d"
    },
    {
      "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": "a15e9f8a-9450-4506-b091-bf3dc8aa66f8"
    },
    {
      "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; return ['❌ <b>RLE</b> · Workflow-level failure at ' + h(E.error_node || E.stage), h(E.summary), 'Error: ' + h(E.error_message), 'Next: open the execution and fix ' + h(E.error_node || 'the failing node') + '; if the lead never reached its card, delete its Data Table row and re-POST it.', '#rle · run <code>' + h(E.correlation_id) + '</code> · ' + h(E.execution_url)].join('\\n'); })() }}",
        "additionalFields": {
          "appendAttribution": false,
          "disable_web_page_preview": true,
          "parse_mode": "HTML"
        }
      },
      "notes": "Fires for production executions that escape per-node error handling, after the Error Log row is written. Reads the error record by node name. Telegram here; swap for Slack/Email if the client alerts elsewhere.",
      "retryOnFail": true,
      "maxTries": 3,
      "waitBetweenTries": 5000,
      "id": "263eb3dc-ce4c-40f6-8cb8-e6fa39f8556c"
    },
    {
      "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; return ['❌ <b>RLE</b> · Workflow-level failure at ' + h(E.error_node || E.stage) + ' (Error Log row NOT written)', h(E.summary), 'Error: ' + h(E.error_message), '⚠️ Error Log row not written: ' + h($json.error?.message ?? $json.error), 'Next: open the execution and fix ' + h(E.error_node || 'the failing node') + '; if the lead never reached its card, delete its Data Table row and re-POST it.', '#rle · run <code>' + h(E.correlation_id) + '</code> · ' + 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 message says the row is missing.",
      "retryOnFail": true,
      "maxTries": 3,
      "waitBetweenTries": 5000,
      "id": "2c2c70fe-09cb-4edb-8515-b9bd899d5e66"
    }
  ],
  "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": {}
}
