{
  "name": "SmartMart Error Handler",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "error",
        "responseMode": "responseNode",
        "responseData": "",
        "options": {}
      },
      "id": "b4cfa0d3-4444-4f60-b071-333333333333",
      "name": "Error webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [0, 0]
    },
    {
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "const body = $input.first().json.body || $input.first().json;\nconst error = body.error || body;\nconst message = error.message || body.message || 'Unknown n8n execution error';\nconst node = error.node || body.nodeName || '';\nconst execId = error.executionId || body.executionId || '';\nreturn [{\n  json: {\n    incidentType: 'integration_error',\n    severity: 'HIGH',\n    riskScore: 70,\n    summary: 'n8n workflow error: ' + String(message).slice(0, 500) + (node ? ' (node: ' + node + ')' : '') + (execId ? ' (execution: ' + execId + ')' : ''),\n    source: 'n8n-error-handler',\n    raw: body\n  }\n}];"
      },
      "id": "b4cfa0d3-4444-4f60-b071-333333333334",
      "name": "Parse error",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [220, 0]
    },
    {
      "parameters": {
        "url": "={{ $env.SMARTMART_BASE_URL }}/api/v1/ai-operations/incidents",
        "authentication": "none",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "Bearer {{ $env.SMARTMART_N8N_TOKEN }}"
            },
            {
              "name": "Cookie",
              "value": "tunnel_phishing_protection=$env.TUNNEL_COOKIE"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({ type: \"integration_error\", severity: \"HIGH\", risk_score: 70, summary: $json.summary, report: $json.raw, status: \"open\", source: \"n8n-error-handler\" }) }}",
        "sendPreflight": false,
        "options": {
          "timeout": 10000
        }
      },
      "id": "b4cfa0d3-4444-4f60-b071-333333333335",
      "name": "Record incident",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [440, 0]
    },
    {
      "parameters": {
        "conditions": {
          "string": {
            "string": [
              {
                "value1": "={{ $env.SLACK_WEBHOOK_URL || '' }}",
                "operation": "notEmpty"
              }
            ]
          }
        },
        "options": {}
      },
      "id": "b4cfa0d3-4444-4f60-b071-333333333336",
      "name": "Slack configured?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.1,
      "position": [660, 0]
    },
    {
      "parameters": {
        "url": "={{ $env.SLACK_WEBHOOK_URL }}",
        "authentication": "none",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({ text: ':rotating_light: SmartMart n8n error - ' + $('Parse error').first().json.summary }) }}",
        "sendPreflight": false,
        "options": {
          "timeout": 10000
        }
      },
      "id": "b4cfa0d3-4444-4f60-b071-333333333337",
      "name": "Alert Slack",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [880, -120]
    },
    {
      "parameters": {},
      "id": "b4cfa0d3-4444-4f60-b071-333333333338",
      "name": "Error recorded",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 0.1,
      "position": [880, 160]
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={{ JSON.stringify({ success: true, incident: $('Record incident').first().json.data }) }}",
        "options": {}
      },
      "id": "b4cfa0d3-4444-4f60-b071-333333333339",
      "name": "Respond to webhook",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [1100, -120]
    }
  ],
  "connections": {
    "Error webhook": {
      "main": [[{ "node": "Parse error", "type": "main", "index": 0 }]]
    },
    "Parse error": {
      "main": [[{ "node": "Record incident", "type": "main", "index": 0 }]]
    },
    "Record incident": {
      "main": [
        [{ "node": "Respond to webhook", "type": "main", "index": 0 }],
        [{ "node": "Slack configured?", "type": "main", "index": 0 }]
      ]
    },
    "Slack configured?": {
      "main": [
        [{ "node": "Alert Slack", "type": "main", "index": 0 }],
        [{ "node": "Error recorded", "type": "main", "index": 0 }]
      ]
    }
  },
  "settings": {
    "timezone": "Africa/Lagos",
    "executionOrder": "v1"
  }
}