8
n8n 中文网amn8n.com

记录错误并避免发送过多邮件

高级

这是一个Engineering, AI, IT Ops领域的自动化工作流,包含 16 个节点。主要使用 If, Code, Postgres, Pushover, EmailSend 等节点,结合人工智能技术实现智能自动化。 带有PostgreSQL日志记录和限速通知的错误处理系统

前置要求
  • PostgreSQL 数据库连接信息
工作流预览
可视化展示节点连接关系,支持缩放和平移
导出工作流
复制以下 JSON 配置到 n8n 导入,即可使用此工作流
{
  "id": "YybYYc430rmZWJPJ",
  "meta": {
    "instanceId": "febfa0961d1e55a48938f0337f348b73a50538aa16673607611ead85d95f662c",
    "templateCredsSetupCompleted": true
  },
  "name": "记录错误并避免发送过多邮件",
  "tags": [
    {
      "id": "7YoU4oTsaGGEtWJj",
      "name": "sample",
      "createdAt": "2025-01-31T16:41:27.407Z",
      "updatedAt": "2025-01-31T16:41:27.407Z"
    }
  ],
  "nodes": [
    {
      "id": "0e44df4c-00d2-4545-89ae-844a590de369",
      "name": "错误触发器",
      "type": "n8n-nodes-base.errorTrigger",
      "position": [
        -1180,
        40
      ],
      "parameters": {},
      "typeVersion": 1
    },
    {
      "id": "7101542a-5146-4917-a1f2-13686cad197e",
      "name": "插入日志",
      "type": "n8n-nodes-base.postgres",
      "position": [
        -960,
        -10
      ],
      "parameters": {
        "table": {
          "__rl": true,
          "mode": "list",
          "value": "N8Err",
          "cachedResultName": "N8Err"
        },
        "schema": {
          "__rl": true,
          "mode": "name",
          "value": "p1gq6ljdsam3x1m"
        },
        "columns": {
          "value": {
            "URL": "={{ $json.execution.url }}",
            "json": "={{ JSON.stringify($json) }}",
            "Stack": "={{ $json.execution.error.stack }}",
            "title": "={{ $json.workflow.name }}",
            "Message": "={{ $json.execution.error.message }}",
            "LastNode": "={{ $json.execution.lastNodeExecuted }}",
            "created_at": "={{ $now }}"
          },
          "schema": [
            {
              "id": "id",
              "type": "number",
              "display": true,
              "removed": true,
              "required": false,
              "displayName": "id",
              "defaultMatch": true,
              "canBeUsedToMatch": true
            },
            {
              "id": "created_at",
              "type": "dateTime",
              "display": true,
              "required": false,
              "displayName": "created_at",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "updated_at",
              "type": "dateTime",
              "display": true,
              "removed": true,
              "required": false,
              "displayName": "updated_at",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "created_by",
              "type": "string",
              "display": true,
              "removed": true,
              "required": false,
              "displayName": "created_by",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "updated_by",
              "type": "string",
              "display": true,
              "removed": true,
              "required": false,
              "displayName": "updated_by",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "nc_order",
              "type": "number",
              "display": true,
              "removed": true,
              "required": false,
              "displayName": "nc_order",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "title",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "title",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "URL",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "URL",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Stack",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "Stack",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "json",
              "type": "object",
              "display": true,
              "required": false,
              "displayName": "json",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Message",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "Message",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "LastNode",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "LastNode",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            }
          ],
          "mappingMode": "defineBelow",
          "matchingColumns": [
            "id"
          ],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {}
      },
      "credentials": {
        "postgres": {
          "id": "2VsRB7eDnG0FA3z2",
          "name": "Postgres Nocodb"
        }
      },
      "typeVersion": 2.6
    },
    {
      "id": "8fb1201c-353e-466c-8d08-fd969e6b10b1",
      "name": "计数5分钟",
      "type": "n8n-nodes-base.postgres",
      "position": [
        -960,
        -210
      ],
      "parameters": {
        "query": "SELECT count(*) FROM p1gq6ljdsam3x1m.\"N8Err\" where created_at >= $1;\n",
        "options": {
          "queryReplacement": "={{ $now.minus(5, 'minutes').toString() }}"
        },
        "operation": "executeQuery"
      },
      "credentials": {
        "postgres": {
          "id": "2VsRB7eDnG0FA3z2",
          "name": "Postgres Nocodb"
        }
      },
      "typeVersion": 2.6
    },
    {
      "id": "89f836dc-8141-4c20-a758-bf7ff261a87b",
      "name": "便签",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -2260,
        -300
      ],
      "parameters": {
        "color": 5,
        "width": 820,
        "height": 1140,
        "content": "# 记录错误并避免发送过多邮件"
      },
      "typeVersion": 1
    },
    {
      "id": "fba7fec5-5285-46bd-9cc7-270b7dcc8c5f",
      "name": "主邮箱",
      "type": "n8n-nodes-base.emailSend",
      "onError": "continueErrorOutput",
      "disabled": true,
      "position": [
        -960,
        300
      ],
      "webhookId": "d76d2e82-b0a8-4e35-88f9-1815d4ce6c79",
      "parameters": {
        "text": "={{ $(\"Error Trigger\").item.json.execution.url }}\n\n{{ $(\"Error Trigger\").item.json.execution.lastNodeExecuted }}\n\n{{ $(\"Error Trigger\").item.json.execution.error.message }}\n{{ $(\"Error Trigger\").item.json.execution.error.stack }}",
        "options": {
          "appendAttribution": false
        },
        "subject": "=Erro -  {{ $(\"Error Trigger\").item.json.workflow.name }}",
        "toEmail": "davimesquita@gmail.com",
        "fromEmail": "suporte@ideias.casa",
        "emailFormat": "text"
      },
      "credentials": {
        "smtp": {
          "id": "0YIoKeISQNR2kxwO",
          "name": "SMTP Resent"
        }
      },
      "typeVersion": 2.1
    },
    {
      "id": "979d0e82-42e8-450a-95b1-3c204ad61a50",
      "name": "备用邮箱",
      "type": "n8n-nodes-base.emailSend",
      "disabled": true,
      "position": [
        -740,
        300
      ],
      "webhookId": "d76d2e82-b0a8-4e35-88f9-1815d4ce6c79",
      "parameters": {
        "text": "={{ $(\"Error Trigger\").item.json.execution.url }}\n\n{{ $(\"Error Trigger\").item.json.execution.lastNodeExecuted }}\n\n{{ $(\"Error Trigger\").item.json.execution.error.message }}\n{{ $(\"Error Trigger\").item.json.execution.error.stack }}",
        "options": {
          "appendAttribution": false
        },
        "subject": "=Erro -  {{ $(\"Error Trigger\").item.json.workflow.name }}",
        "toEmail": "davimesquita@gmail.com",
        "fromEmail": "contato@ideias.casa",
        "emailFormat": "text"
      },
      "credentials": {
        "smtp": {
          "id": "UvWloRL7Jyqt8tm9",
          "name": "SMTP Contato"
        }
      },
      "typeVersion": 2.1
    },
    {
      "id": "6c073c03-e00e-45b1-8f14-faa29fd58472",
      "name": "推送移动通知",
      "type": "n8n-nodes-base.pushover",
      "disabled": true,
      "position": [
        -960,
        500
      ],
      "parameters": {
        "message": "={{ $(\"Error Trigger\").item.json.workflow.name }} - {{ $(\"Error Trigger\").item.json.execution.url }}\n\n{{ $(\"Error Trigger\").item.json.execution.lastNodeExecuted }}\n\n{{ $(\"Error Trigger\").item.json.execution.error.message }}\n{{ $(\"Error Trigger\").item.json.execution.error.stack }}",
        "userKey": "=u4RMqXQR9EFdeSQBfaL1riBy1Qd953",
        "additionalFields": {}
      },
      "credentials": {
        "pushoverApi": {
          "id": "ae8Jsj87n2hSWDbs",
          "name": "Pushover account"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "4ca939e4-dcb1-40bd-b5eb-4cd00cb403fb",
      "name": "清空日志数据库",
      "type": "n8n-nodes-base.postgres",
      "position": [
        -960,
        900
      ],
      "parameters": {
        "table": {
          "__rl": true,
          "mode": "list",
          "value": "N8Err",
          "cachedResultName": "N8Err"
        },
        "schema": {
          "__rl": true,
          "mode": "list",
          "value": "p1gq6ljdsam3x1m",
          "cachedResultName": "p1gq6ljdsam3x1m"
        },
        "options": {},
        "operation": "deleteTable",
        "restartSequences": true
      },
      "credentials": {
        "postgres": {
          "id": "2VsRB7eDnG0FA3z2",
          "name": "Postgres Nocodb"
        }
      },
      "typeVersion": 2.6
    },
    {
      "id": "1eaf67ca-fb77-4b76-8ee3-ae65d4b79182",
      "name": "有时...只需清理",
      "type": "n8n-nodes-base.manualTrigger",
      "position": [
        -1200,
        900
      ],
      "parameters": {},
      "typeVersion": 1
    },
    {
      "id": "01e5a7dd-41a2-43f1-bbf5-241e6791cf18",
      "name": "调用此示例 - 前置到您的错误捕获器",
      "type": "n8n-nodes-base.executeWorkflow",
      "disabled": true,
      "position": [
        -1180,
        400
      ],
      "parameters": {
        "options": {},
        "workflowId": {
          "__rl": true,
          "mode": "id",
          "value": ""
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "4386788d-5f10-468a-8a02-cff45a4a7ed5",
      "name": "参见下方将此前置到您的错误处理中",
      "type": "n8n-nodes-base.executeWorkflowTrigger",
      "position": [
        -1180,
        -260
      ],
      "parameters": {
        "inputSource": "passthrough"
      },
      "typeVersion": 1.1
    },
    {
      "id": "d6aed974-4a36-4edd-809d-867a95d0f6ef",
      "name": "如果5分钟内没有日志",
      "type": "n8n-nodes-base.if",
      "position": [
        -740,
        -210
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "loose"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "a17b915d-f581-4774-a78a-48bc386aebc9",
              "operator": {
                "type": "number",
                "operation": "lte"
              },
              "leftValue": "={{ $json.count }}",
              "rightValue": 0
            }
          ]
        },
        "looseTypeValidation": true
      },
      "typeVersion": 2.2
    },
    {
      "id": "3c49f611-f1a6-409a-a4c6-903dadb27165",
      "name": "清理执行。如果您要前置此工作流,请参见下方",
      "type": "n8n-nodes-base.code",
      "position": [
        -520,
        -210
      ],
      "parameters": {
        "jsCode": "return [];"
      },
      "typeVersion": 2
    },
    {
      "id": "192443fc-c032-4815-acc7-c8cf6040cc34",
      "name": "在此之后插入您的错误处理逻辑",
      "type": "n8n-nodes-base.noOp",
      "position": [
        -300,
        -210
      ],
      "parameters": {},
      "typeVersion": 1
    },
    {
      "id": "2f87907f-816f-4054-8517-bb713a203131",
      "name": "便签1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1300,
        240
      ],
      "parameters": {
        "width": 840,
        "height": 460,
        "content": "# 错误处理示例"
      },
      "typeVersion": 1
    },
    {
      "id": "b173898f-d1d8-4f83-b7b7-ba52cab7651e",
      "name": "便签2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1300,
        720
      ],
      "parameters": {
        "width": 1140,
        "height": 340,
        "content": "# 数据库清理:在开发环境中有用,但请勿在生产环境中运行"
      },
      "typeVersion": 1
    }
  ],
  "active": false,
  "pinData": {
    "Error Trigger": [
      {
        "json": {
          "workflow": {
            "id": "1",
            "name": "Example Workflow"
          },
          "execution": {
            "id": 231,
            "url": "https://work.ideias.casa/execution/workflow/1/231",
            "mode": "manual",
            "error": {
              "stack": "Stacktrace",
              "message": "Example Error Message"
            },
            "retryOf": "34",
            "lastNodeExecuted": "Node With Error"
          }
        }
      }
    ]
  },
  "settings": {
    "callerPolicy": "workflowsFromSameOwner",
    "executionOrder": "v1",
    "saveManualExecutions": false,
    "saveExecutionProgress": false,
    "saveDataErrorExecution": "all",
    "saveDataSuccessExecution": "none"
  },
  "versionId": "f5e80ccb-c64f-47bd-89a6-55cf3e85a572",
  "connections": {
    "Insert Log": {
      "main": [
        []
      ]
    },
    "Error Trigger": {
      "main": [
        [
          {
            "node": "Insert Log",
            "type": "main",
            "index": 0
          },
          {
            "node": "Count for 5 minutes",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Principal E-Mail": {
      "main": [
        [],
        [
          {
            "node": "Fallback E-Mail",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Count for 5 minutes": {
      "main": [
        [
          {
            "node": "If there is no logs in 5 minutes",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Sometimes... just cleanup": {
      "main": [
        [
          {
            "node": "Truncate Log Database",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "If there is no logs in 5 minutes": {
      "main": [
        [],
        [
          {
            "node": "CleanUp execution. See below if you will prepend this workflow",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Call this Sample - Prepend to your error catcher": {
      "main": [
        [
          {
            "node": "Principal E-Mail",
            "type": "main",
            "index": 0
          },
          {
            "node": "Push mobile notification",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "See below to prepend this at your error handling": {
      "main": [
        [
          {
            "node": "Insert Log",
            "type": "main",
            "index": 0
          },
          {
            "node": "Count for 5 minutes",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "CleanUp execution. See below if you will prepend this workflow": {
      "main": [
        [
          {
            "node": "Insert your error handling logic after this",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
常见问题

如何使用这个工作流?

复制上方的 JSON 配置代码,在您的 n8n 实例中创建新工作流并选择「从 JSON 导入」,粘贴配置后根据需要修改凭证设置即可。

这个工作流适合什么场景?

高级 - 工程, 人工智能, IT 运维

需要付费吗?

本工作流完全免费,您可以直接导入使用。但请注意,工作流中使用的第三方服务(如 OpenAI API)可能需要您自行付费。

工作流信息
难度等级
高级
节点数量16
分类3
节点类型11
难度说明

适合高级用户,包含 16+ 个节点的复杂工作流

作者
Davi Saranszky Mesquita

Davi Saranszky Mesquita

@frkr
外部链接
在 n8n.io 查看

分享此工作流