{
  "id": "t0BzAIMjEGnkHiK1",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "name": "Template - Global Error Handler",
  "tags": [],
  "nodes": [
    {
      "id": "39547c77-3ae8-4763-95e9-750eab1a1d61",
      "name": "Error Trigger",
      "type": "n8n-nodes-base.errorTrigger",
      "position": [
        -256,
        176
      ],
      "parameters": {},
      "typeVersion": 1
    },
    {
      "id": "d6ddde96-7ed1-4498-868c-ab68496a4823",
      "name": "Send email",
      "type": "n8n-nodes-base.emailSend",
      "position": [
        912,
        192
      ],
      "parameters": {
        "html": "={{ $node['Format Email Body'].json.emailBody }}",
        "options": {},
        "subject": "={{ $node['Config - Set Fields'].json.emailSubject }}",
        "toEmail": "={{ $node['Config - Set Fields'].json.toEmail }}",
        "fromEmail": "={{ $node['Config - Set Fields'].json.fromEmail }}"
      },
      "credentials": {
        "smtp": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2.1
    },
    {
      "id": "71d27f66-0026-41c1-8c59-d4a78f5bd23f",
      "name": "Format Email Body",
      "type": "n8n-nodes-base.set",
      "position": [
        688,
        192
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "ea244a95-1fc6-477e-8cdb-7a6fd468b28b",
              "name": "emailBody",
              "type": "string",
              "value": "=<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n  <meta charset=\"UTF-8\" />\n  <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\" />\n  <title>Workflow Error Alert</title>\n  <style>\n    body {\n      font-family: Arial, sans-serif;\n      background-color: #f4f6f8;\n      margin: 0;\n      padding: 0;\n    }\n    .container {\n      max-width: 600px;\n      background: #ffffff;\n      margin: 30px auto;\n      padding: 20px;\n      border-radius: 8px;\n      border: 1px solid #d1d5db;\n    }\n    .header {\n      background-color: #dc3545;\n      color: white;\n      padding: 15px 0px;\n      border-radius: 8px 8px 0 0;\n      font-size: 18px;\n      font-weight: bold;\n      text-align: center;\n    }\n    .content {\n      padding: 15px;\n      color: #333333;\n      line-height: 1.5;\n    border: 0.5px solid #cfcfcf\n     \n    }\n    .label {\n      font-weight: bold;\n      color: #555555;\n    }\n    .footer {\n      margin-top: 25px;\n      font-size: 12px;\n      color: #888888;\n      text-align: center;\n    }\n    a.button {\n      display: inline-block;\n      background-color: #dc3545;\n      color: white !important;\n      padding: 10px 20px;\n      border-radius: 5px;\n      text-decoration: none;\n      margin-top: 15px;\n    }\n    a.button:hover {\n      background-color: #c82333;\n    }\n  </style>\n</head>\n<body>\n  \n  <div class=\"container\">\n    <div class=\"header\">\n      Workflow Error Notification\n    </div>\n    <div class=\"content\">\n      <p>Dear User,</p>\n      <p>An error occurred in one of your n8n workflows. Please review the details below:</p>\n\n      <!-- Table for Workflow Details -->\n      <center>\n      <table  style=\"width:95%; table-layout: fixed; border-collapse: collapse;\" border=\"1\" cellpadding=\"8\" cellspacing=\"0\">\n        <tr>\n          <td class=\"label\"><nobr>Workflow Name:</nobr></td>\n          <td>{{ $node['Error Trigger'].json.workflow.name || \"Unknown\" }}</td>\n        </tr>\n        <tr>\n          <td class=\"label\"><nobr>Workflow ID:</nobr></td>\n          <td>{{ $node['Error Trigger'].json.workflow.id || \"N/A\" }}</td>\n        </tr>\n        <tr>\n          <td class=\"label\"><nobr>Node Name:</nobr></td>\n          <td>{{ $node['Error Trigger'].json.execution.lastNodeExecuted || \"Unknown\" }}</td>\n        </tr>\n        <tr>\n          <td class=\"label\"><nobr>Run ID:</nobr></td>\n          <td>{{ $node['Error Trigger'].json.execution.id || \"N/A\" }}</td>\n        </tr>\n        <tr>\n          <td class=\"label\"><nobr>Severity Level:</nobr></td>\n          <td>{{ $if(\n    $(\"Analyze Error with AI\").isExecuted, $node['Analyze Error with AI'].json.output[0].content[0].text.parseJson().severity_level, \"\") }}</td>\n        </tr>\n        <tr>\n          <td class=\"label\"><nobr>Error Message:</nobr></td>\n          <td>{{ $node['Error Trigger'].json.execution.error.message || \"\" }}</td>\n        </tr>\n        <tr>\n          <td class=\"label\"><nobr>Quick Resolution:</nobr></td>\n          <td>{{ $if(\n    $(\"Analyze Error with AI\").isExecuted, $node['Analyze Error with AI'].json.output[0].content[0].text.parseJson().quick_resolution, \"\") }}</td>\n        </tr>\n      </table>\n      </center>\n\n      <a href=\"{{ $node['Error Trigger'].json.execution.url }}\" target=\"_blank\" class=\"button\">Open Workflow</a>\n    </div>\n    <div class=\"footer\">\n      This is an automated message from your n8n automation system.\n    </div>\n  </div>\n</body>\n</html>\n"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "e0c99c23-e469-4e33-8fcc-2fef81715011",
      "name": "Config - Set Fields",
      "type": "n8n-nodes-base.set",
      "position": [
        -80,
        176
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "cbab20d4-6827-4ef7-9070-1c73e2405623",
              "name": "fromEmail",
              "type": "string",
              "value": "user@example.com"
            },
            {
              "id": "2bebfc49-e340-4d34-9e58-8c4ed0066ce6",
              "name": "toEmail",
              "type": "string",
              "value": "user@example.com"
            },
            {
              "id": "9de9fce9-c45c-401a-b1d1-16dd21ba339d",
              "name": "emailSubject",
              "type": "string",
              "value": "=\ud83e\udd16 N8N Workflow Error Alert: {{ $node['Error Trigger'].json.workflow.name }}"
            },
            {
              "id": "3c39e3fb-9761-468e-a929-e530a32ad7f4",
              "name": "AnalyzeErrorWithAI",
              "type": "boolean",
              "value": false
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "acac5643-5e99-4240-88bb-fd3eb0873e1b",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1008,
        -272
      ],
      "parameters": {
        "width": 672,
        "height": 640,
        "content": "## Overview \u2014 Error Alerts with Optional AI Insights\n\nThis workflow sends an email notification whenever any workflow in your n8n instance encounters an error. It\u2019s built for developers, operators, or teams who want instant visibility when something breaks with the flexibility to enrich alerts using AI. The workflow includes an optional **\u201cUse AI Analysis?\u201d** switch that lets users decide whether they want an AI-generated severity level and quick resolution for faster troubleshooting.\n\n### How it works\n**1.** The **Error Trigger** captures details whenever a workflow fails.\n**2.** The **Config \u2014 Set Fields** lets you define email settings and toggle AI analysis.  \n**3.** **Use AI Analysis?** decides the path:\n**(i) True:** The workflow sends the error to the AI node for interpretation. You receive a Severity Level and Quick Resolution along with the detailed error.\n**(ii) False:** You receive a simple, clean error notification without AI suggestions.\n**4.** The **Format Email Body** prepares the email based on the selected mode.  \n**5.** The **Send Email** delivers the alert using your SMTP credentials.\n\n### Setup steps\n**1.** Fill email_to, email_from, email_subject, and AnalyzeErrorWithAI in the Config node.\n**2.** Connect SMTP credentials. \n**3.** If using AI, connect OpenAI credentials and set your preferred model.\n**4.** Save and activate the workflow.\n\n### Customization\nYou can enrich your alert with execution data, workflow IDs, or even send parallel alerts to Slack, Telegram, or Discord. This workflow serves as a plug-and-play template to build a complete monitoring system for your automations.\n"
      },
      "typeVersion": 1
    },
    {
      "id": "6c91c6cf-d852-44d7-ac32-c3a4b4516c20",
      "name": "Analyze Error with AI",
      "type": "@n8n/n8n-nodes-langchain.openAi",
      "position": [
        352,
        64
      ],
      "parameters": {
        "modelId": {
          "__rl": true,
          "mode": "list",
          "value": "gpt-4o-mini",
          "cachedResultName": "GPT-4O-MINI"
        },
        "options": {},
        "responses": {
          "values": [
            {
              "role": "system",
              "content": "You are an expert automation engineer. \nAlways respond with a RAW JSON object without wrapping it in quotes. \nNever escape characters, never add \\n, never format JSON as a string.\nNever include explanations, markdown, commentary, or prose.\nreturn ONLY valid JSON that includes:\n- severity level\n- a detailed (not more than 100 words) suggested configuration/change\nDo NOT include explanations, prose, markdown, commentary, or any text before or after the JSON."
            },
            {
              "content": "=Analyze the following workflow error and return ONLY a valid JSON object (do not include \\n etc...) with this exact structure:\n\n{\n  \"severity_level\": \"\",\n  \"quick_resolution\": \"\"\n}\n\n### Context:\nWorkflow Name: {{ $node['Error Trigger'].json.workflow.name || \"Unknown\" }}\nWorkflow ID: {{ $node['Error Trigger'].json.workflow.id || \"N/A\" }}\nNode Name: {{ $node['Error Trigger'].json.execution.lastNodeExecuted || \"Unknown\" }}\nRun ID: {{ $node['Error Trigger'].json.execution.id || \"N/A\" }}\nError Message: {{ $node['Error Trigger'].json.execution.error.message }}\n\n\n\n### Requirements:\n1. \"severity_level\" must be one of: Low, Medium, High, Critical.\n2. \"quick_resolution\" must be a  detailed (not more than 100 words), practical fix the user can apply.\n3. The response MUST be valid JSON and nothing else.\n\nReturn the JSON object directly, not as a string. Do not escape characters.\n"
            }
          ]
        },
        "builtInTools": {}
      },
      "credentials": {
        "openAiApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2.1
    },
    {
      "id": "3a5f3ecb-f751-411f-8297-7195401d0392",
      "name": "Use AI Analysis?",
      "type": "n8n-nodes-base.if",
      "position": [
        128,
        176
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "e9cc54ff-e634-490f-9b33-06f79a800653",
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              },
              "leftValue": "={{ $node['Config - Set Fields'].json.AnalyzeErrorWithAI }}",
              "rightValue": ""
            }
          ]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "292a1c7d-b126-4bd1-b2ad-7a4be5e58228",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -256,
        -272
      ],
      "parameters": {
        "width": 464,
        "height": 320,
        "content": "## How to Use This Workflow\n@[youtube](nzEApie96xk)"
      },
      "typeVersion": 1
    }
  ],
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "0e42172d-b6f3-4e90-a90a-7c152aa7d5a9",
  "connections": {
    "Error Trigger": {
      "main": [
        [
          {
            "node": "Config - Set Fields",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Use AI Analysis?": {
      "main": [
        [
          {
            "node": "Analyze Error with AI",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Format Email Body",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Format Email Body": {
      "main": [
        [
          {
            "node": "Send email",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Config - Set Fields": {
      "main": [
        [
          {
            "node": "Use AI Analysis?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Analyze Error with AI": {
      "main": [
        [
          {
            "node": "Format Email Body",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}