WFDASH-WEBAPP/scripts/n8n-v2-error.ts

85 lines
2.6 KiB
TypeScript
Raw Permalink Normal View History

import { workflow, node, trigger, newCredential, expr } from '@n8n/workflow-sdk';
const errorTrigger = trigger({
type: 'n8n-nodes-base.errorTrigger',
version: 1,
config: {
name: 'Error Trigger',
position: [240, 300]
},
output: [{ error: { message: 'e' }, execution: { id: '1', lastNodeExecuted: 'n' }, subject: 's', from_email: 'x@y', uid: '123', mailbox: 'INBOX' }]
});
const buildAlert = node({
type: 'n8n-nodes-base.code',
version: 2,
config: {
name: 'Build Alert',
parameters: {
mode: 'runOnceForEachItem',
jsCode: `const errorInfo = $json.error || {};
const execution = $json.execution || {};
const lastNode = execution.lastNodeExecuted || 'unknown';
const errorMessage = errorInfo.message || 'Unknown error';
const subject = $json.subject || '(no subject)';
const fromEmail = $json.from_email || '(unknown sender)';
const uid = String($json.uid || '');
const mailbox = $json.mailbox || 'INBOX';
const alertText = '⚠️ WFDASH Quote Email Error\\n\\n' +
'From: ' + fromEmail + '\\n' +
'Subject: ' + subject + '\\n' +
'UID: ' + (uid || '(no uid)') + '\\n' +
'Failed at: ' + lastNode + '\\n' +
'Error: ' + errorMessage + '\\n' +
'Execution: ' + (execution.id || 'unknown') + '\\n\\n' +
'Email is still in ' + mailbox + ' — please triage manually.';
return {
json: {
uid: uid,
mailbox: mailbox,
alert_text: alertText,
subject: subject,
from_email: fromEmail
}
};`
},
position: [540, 300]
},
output: [{ uid: '123', mailbox: 'INBOX', alert_text: 'a', subject: 's', from_email: 'x@y' }]
});
// v2 error workflow has no IMAP Move-to-Failed step. The n8n JS task-runner
// image (`n8nio/runners:2.25.7`) blocks `require('imapflow')` outright, and
// there is no env-var to allowlist new modules in that sandbox. The failing
// email stays where it was (likely `INBOX`); the next poll re-fires the main
// workflow; the human sees the Telegram alert and triages the email.
const telegramAlert = node({
type: 'n8n-nodes-base.telegram',
version: 1.2,
config: {
name: 'Telegram Alert',
parameters: {
resource: 'message',
operation: 'sendMessage',
chatId: expr("{{ $env.TELEGRAM_CHAT_ID }}"),
text: expr("{{ $json.alert_text }}"),
additionalFields: {
parse_mode: 'HTML',
disable_web_page_preview: true
}
},
position: [1140, 300],
credentials: { telegramApi: newCredential('Telegram Alerts') }
},
output: [{ ok: true, result: { message_id: 1 } }]
});
export default workflow('wfdash-error-handler', 'WFDASH — Error Handler')
.add(errorTrigger)
.to(buildAlert)
.to(telegramAlert);