{
  "id": "CODWF99ErrorHandler",
  "name": "COD · WF-99 Error Handler",
  "active": false,
  "nodes": [
    {
      "parameters": {
        "content": "## Global error boundary\nReceives failed workflow executions, alerts the administrator, and records an audit event through the internal API.",
        "height": 210,
        "width": 900,
        "color": 3
      },
      "id": "wf99-note-layer",
      "name": "Layer 99 · Error Boundary",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -500,
        -260
      ]
    },
    {
      "parameters": {},
      "id": "wf99-trigger",
      "name": "Workflow Error Trigger",
      "type": "n8n-nodes-base.errorTrigger",
      "typeVersion": 1,
      "position": [
        -440,
        20
      ]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "wf99-message",
              "name": "errorMessage",
              "value": "={{ '🚨 n8n workflow failure\\nWorkflow: ' + ($json.workflow?.name || 'Unknown') + '\\nExecution: ' + ($json.execution?.id || '-') + '\\nError: ' + ($json.execution?.error?.message || $json.error?.message || 'Unknown error') }}",
              "type": "string"
            },
            {
              "id": "wf99-order-id",
              "name": "orderId",
              "value": "={{ $json.execution?.lastNodeExecuted ? undefined : $json.orderId }}",
              "type": "string"
            }
          ]
        },
        "includeOtherFields": true,
        "options": {}
      },
      "id": "wf99-format",
      "name": "Format Error",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        -200,
        20
      ]
    },
    {
      "parameters": {
        "resource": "message",
        "operation": "sendMessage",
        "chatId": "={{ $env.TELEGRAM_ADMIN_CHAT_ID }}",
        "text": "={{ $json.errorMessage }}",
        "additionalFields": {
          "appendAttribution": false
        }
      },
      "id": "wf99-alert",
      "name": "Admin Telegram Error Alert",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        40,
        20
      ],
      "continueOnFail": true,
      "credentials": {
        "telegramApi": {
          "id": "cred_telegram",
          "name": "COD Telegram Bot"
        }
      }
    },
    {
      "parameters": {
        "method": "POST",
        "url": "={{ $env.INTERNAL_API_URL }}/api/internal/events",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendBody": true,
        "contentType": "raw",
        "rawContentType": "application/json",
        "body": "={{ JSON.stringify({ orderId: $('Format Error').item.json.orderId || undefined, type: 'WORKFLOW_ERROR', actor: 'n8n', message: $('Format Error').item.json.errorMessage, meta: { workflow: $('Format Error').item.json.workflow?.name || null, executionId: $('Format Error').item.json.execution?.id || null } }) }}",
        "options": {
          "timeout": 10000
        }
      },
      "id": "wf99-log",
      "name": "Log Workflow Error Event",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        280,
        20
      ],
      "retryOnFail": true,
      "maxTries": 3,
      "waitBetweenTries": 1000,
      "credentials": {
        "httpHeaderAuth": {
          "id": "cred_internal_api",
          "name": "COD Internal API Bearer"
        }
      }
    }
  ],
  "connections": {
    "Workflow Error Trigger": {
      "main": [
        [
          {
            "node": "Format Error",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Format Error": {
      "main": [
        [
          {
            "node": "Admin Telegram Error Alert",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Admin Telegram Error Alert": {
      "main": [
        [
          {
            "node": "Log Workflow Error Event",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1",
    "timezone": "Africa/Cairo"
  },
  "versionId": "f9900000-0000-4000-8000-000000000000",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "tags": []
}
