-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path98-error-handler.json
More file actions
245 lines (245 loc) · 8.84 KB
/
Copy path98-error-handler.json
File metadata and controls
245 lines (245 loc) · 8.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
{
"name": "98 - Global Error Handler (dead-letter + Telegram/email)",
"nodes": [
{
"parameters": {},
"id": "error-trigger",
"name": "Error Trigger",
"type": "n8n-nodes-base.errorTrigger",
"typeVersion": 1,
"position": [180, 300]
},
{
"parameters": {
"operation": "executeQuery",
"query": "INSERT INTO n8n_failure_events\n (workflow_id, workflow_name, execution_id, last_node, error_message, raw)\nVALUES (\n '{{ $json.workflow.id || \"\" }}',\n '{{ $json.workflow.name || \"unknown\" }}',\n '{{ $json.execution.id || \"\" }}',\n '{{ $json.execution.lastNodeExecuted || \"\" }}',\n '{{ ($json.error.message || \"unknown error\").replace(/'/g, \"''\") }}',\n '{{ JSON.stringify($json).replace(/'/g, \"''\") }}'::jsonb\n)\nRETURNING id, error_signature;",
"options": {}
},
"id": "insert-dead-letter",
"name": "Insert n8n failure event",
"type": "n8n-nodes-base.postgres",
"typeVersion": 2.5,
"position": [420, 300],
"onError": "continueRegularOutput",
"credentials": {
"postgres": {
"id": "pwwVNTLBw43PJgey",
"name": "Sergeant Postgres (sergeant-db)"
}
}
},
{
"parameters": {
"operation": "executeQuery",
"query": "SELECT COUNT(*) AS prior_alerts\nFROM n8n_failure_events\nWHERE workflow_id = '{{ $('Error Trigger').first().json.workflow.id || \"\" }}'\n AND error_signature = '{{ $json.error_signature }}'\n AND created_at > NOW() - INTERVAL '30 minutes'\n AND id <> {{ $json.id }};",
"options": {}
},
"id": "check-cooldown",
"name": "Check 30-min cooldown",
"type": "n8n-nodes-base.postgres",
"typeVersion": 2.5,
"position": [620, 300],
"onError": "continueRegularOutput",
"credentials": {
"postgres": {
"id": "pwwVNTLBw43PJgey",
"name": "Sergeant Postgres (sergeant-db)"
}
}
},
{
"parameters": {
"conditions": {
"options": {
"caseSensitive": true,
"leftValue": "",
"typeValidation": "strict"
},
"conditions": [
{
"id": "prior-alerts-zero",
"leftValue": "={{ Number($json.prior_alerts ?? 0) }}",
"rightValue": 0,
"operator": {
"type": "number",
"operation": "equals"
}
}
],
"combinator": "and"
},
"options": {}
},
"id": "if-cooldown-elapsed",
"name": "IF cooldown elapsed",
"type": "n8n-nodes-base.if",
"typeVersion": 2,
"position": [820, 300]
},
{
"parameters": {
"jsCode": "// ADR-0038, Wave 3 §3.2 — pre-Telegram step that builds a stable\n// `alertId` keyed on (failed_workflow_id, error_signature) so each\n// failure class produces exactly one ack-row per 30-min cooldown\n// window (WF-98's SQL cooldown already filters at this granularity).\n//\n// WF-98 has no errorWorkflow set, so the chained POST /alerts/post\n// failing cannot loop back here.\nconst err = $('Error Trigger').first()?.json ?? {};\nconst inserted = $('Insert n8n failure event').first()?.json ?? {};\nconst erroredWorkflowId = err.workflow?.id || 'unknown';\nconst erroredWorkflowName = err.workflow?.name || 'unknown';\nconst erroredExecutionId = err.execution?.id || 'unknown';\nconst lastNode = err.execution?.lastNodeExecuted || 'unknown';\nconst errorMessage = err.execution?.error?.message || err.error?.message || 'unknown error';\nconst errorSignature = inserted.error_signature || 'no-signature';\nconst alertId = `wf98:${erroredWorkflowId}:${errorSignature}`;\nconst summary = `n8n workflow failed — ${erroredWorkflowName} @ ${lastNode}: ${errorMessage}`;\nreturn [{\n json: {\n alertId,\n summary,\n metadata: {\n workflowId: $workflow.id || 'unknown',\n executionId: $execution.id || 'unknown',\n erroredWorkflowId,\n erroredWorkflowName,\n erroredExecutionId,\n lastNode,\n errorMessage,\n errorSignature,\n failureEventId: inserted.id ?? null,\n },\n },\n}];"
},
"id": "build-alert-payload-98",
"name": "Build alert payload (n8n-failure)",
"type": "n8n-nodes-base.code",
"typeVersion": 2,
"position": [1040, 220]
},
{
"parameters": {
"method": "POST",
"url": "={{ $env.PUBLIC_API_BASE_URL || 'https://api.invalid.local' }}/api/internal/alerts/post",
"sendHeaders": true,
"headerParameters": {
"parameters": [
{
"name": "Authorization",
"value": "=Bearer {{ $env.INTERNAL_API_KEY }}"
},
{
"name": "Content-Type",
"value": "application/json"
}
]
},
"sendBody": true,
"specifyBody": "json",
"jsonBody": "={\n \"alertId\": \"{{ $json.alertId }}\",\n \"topic\": \"meta\",\n \"severity\": \"P0\",\n \"summary\": \"{{ $json.summary }}\",\n \"metadata\": {{ JSON.stringify($json.metadata) }}\n}",
"options": {
"response": {
"response": {
"neverError": true,
"fullResponse": false
}
}
}
},
"id": "post-alert-row-98",
"name": "POST /api/internal/alerts/post (n8n-failure)",
"type": "n8n-nodes-base.httpRequest",
"typeVersion": 4.2,
"position": [1240, 220],
"onError": "continueRegularOutput"
},
{
"parameters": {
"chatId": "={{ $env.TELEGRAM_ALERT_CHAT_ID }}",
"text": "=🚨 *n8n workflow failed*\n\nWorkflow: `{{ $('Error Trigger').first().json.workflow.name || 'unknown' }}`\nExecution: `{{ $('Error Trigger').first().json.execution.id || 'unknown' }}`\nNode: `{{ $('Error Trigger').first().json.execution.lastNodeExecuted || 'unknown' }}`\nError: {{ $('Error Trigger').first().json.execution.error.message || 'unknown error' }}",
"additionalFields": {
"parse_mode": "Markdown",
"message_thread_id": "={{ $env.TELEGRAM_TOPIC_META }}",
"reply_markup": "={\"inline_keyboard\":[[{\"text\":\"✅ Прочитав\",\"callback_data\":\"ack:r:{{ $('Build alert payload (n8n-failure)').item.json.alertId }}\"},{\"text\":\"🔄 Розбираю\",\"callback_data\":\"ack:i:{{ $('Build alert payload (n8n-failure)').item.json.alertId }}\"},{\"text\":\"🔕 Замутити 30хв\",\"callback_data\":\"ack:m:{{ $('Build alert payload (n8n-failure)').item.json.alertId }}\"}]]}"
}
},
"id": "telegram-alert",
"name": "Telegram failure alert",
"type": "n8n-nodes-base.telegram",
"typeVersion": 1.2,
"position": [1440, 220],
"credentials": {
"telegramApi": {
"id": "ggxeCc1ALZKLuMPV",
"name": "Telegram (Sergeant_alert_bot)"
}
}
},
{
"parameters": {},
"id": "noop-suppressed",
"name": "Suppressed (within cooldown)",
"type": "n8n-nodes-base.noOp",
"typeVersion": 1,
"position": [1040, 400],
"notes": "Alert suppressed: identical (workflow_id, error_signature) was already alerted in the last 30 min. Event is logged in n8n_failure_events but does not page Telegram."
}
],
"connections": {
"Error Trigger": {
"main": [
[
{
"node": "Insert n8n failure event",
"type": "main",
"index": 0
}
]
]
},
"Insert n8n failure event": {
"main": [
[
{
"node": "Check 30-min cooldown",
"type": "main",
"index": 0
}
]
]
},
"Check 30-min cooldown": {
"main": [
[
{
"node": "IF cooldown elapsed",
"type": "main",
"index": 0
}
]
]
},
"IF cooldown elapsed": {
"main": [
[
{
"node": "Build alert payload (n8n-failure)",
"type": "main",
"index": 0
}
],
[
{
"node": "Suppressed (within cooldown)",
"type": "main",
"index": 0
}
]
]
},
"Build alert payload (n8n-failure)": {
"main": [
[
{
"node": "POST /api/internal/alerts/post (n8n-failure)",
"type": "main",
"index": 0
}
]
]
},
"POST /api/internal/alerts/post (n8n-failure)": {
"main": [
[
{
"node": "Telegram failure alert",
"type": "main",
"index": 0
}
]
]
}
},
"settings": {
"executionOrder": "v1",
"timezone": "Europe/Kyiv"
},
"active": false,
"tags": [
{
"name": "ops"
},
{
"name": "dead-letter"
}
]
}