MinAI - Về trang chủ
Dự án
12/1345 phút
Đang tải...

Error Handling & Monitoring

Xử lý lỗi, retry logic, logging, và monitoring production workflows

Error Handling & Monitoring

Production workflows PHẢI handle errors gracefully. Bài cuối này cover error handling, logging, monitoring, và capstone project.

0

🎯 Mục tiêu bài học

TB5 min

🎯 Mục tiêu

  • Error handling patterns trong n8n
  • Retry logic
  • Logging & alerting
  • Production best practices
  • Capstone: Build complete automation system

Checkpoint

Tại sao error handling là bắt buộc cho production workflows?

1

❌ Error Types in n8n

TB5 min

1. Error Types in n8n

1.1 Common Errors

ErrorCauseFix
Connection errorsAPI down, network issueRetry with backoff
Auth errorsExpired token, wrong keyRefresh credentials
Rate limitToo many requestsSlow down, batch
Data errorsMissing field, wrong typeValidate before processing
TimeoutSlow API, large payloadIncrease timeout, chunk data

1.2 Error Flow in n8n

Ví dụ
1Node executes → Success → Next node
2
3 Error → Error output (if configured)
4
5 OR → Workflow stops (default)

Checkpoint

Kể ra 5 loại error phổ biến trong n8n và cách fix cho mỗi loại.

2

🛡️ Error Handling Patterns

TB5 min

2. Error Handling Patterns

2.1 Continue on Fail (Node Setting)

Ví dụ
1Node Settings → Always Output Data → ON
2Node Settings → Continue on Fail → ON
3
4When error occurs:
5- Workflow continues
6- Error info available in $json.error
7- Next node decides what to do

2.2 Error Output (Branching)

🌐HTTP Request node
Success output → Process data
Error output → Handle error

Error output contains:

JSON
1{
2 "error": {
3 "message": "Request failed with status code 429",
4 "stack": "..."
5 }
6}

2.3 IF Node for Error Routing

Ví dụ
1HTTP Request (Continue on Fail)
2→ IF ($json.error exists?)
3 → TRUE: Log error → Slack alert → Stop
4 → FALSE: Process data → Continue

2.4 Error Workflow (Global)

Ví dụ
1Settings → Error Workflow → Select workflow
2
3This workflow runs when ANY execution fails:
4Error Trigger → Format error → Slack alert → Email admin

Checkpoint

"Continue on Fail" và "Error Workflow" khác nhau thế nào? Khi nào dùng cái nào?

3

🔄 Retry Logic

TB5 min

3. Retry Logic

3.1 Built-in Retry

Ví dụ
1Node Settings → Retry on Fail → ON
2- Max Retries: 3
3- Wait Between (ms): 1000
4- Exponential Backoff: ON (1s, 2s, 4s)

3.2 Custom Retry with Code

JavaScript
1// Code node: Custom retry with exponential backoff
2const maxRetries = 3;
3const baseDelay = 1000;
4
5async function fetchWithRetry(url, options) {
6 for (let attempt = 0; attempt < maxRetries; attempt++) {
7 try {
8 const response = await fetch(url, options);
9
10 if (response.status === 429) {
11 // Rate limited - wait and retry
12 const delay = baseDelay * Math.pow(2, attempt);
13 await new Promise(r => setTimeout(r, delay));
14 continue;
15 }
16
17 if (!response.ok) {
18 throw new Error(`HTTP ${response.status}`);
19 }
20
21 return await response.json();
22 } catch (error) {
23 if (attempt === maxRetries - 1) throw error;
24
25 const delay = baseDelay * Math.pow(2, attempt);
26 await new Promise(r => setTimeout(r, delay));
27 }
28 }
29}
30
31const data = await fetchWithRetry(
32 'https://api.example.com/data',
33 { headers: { 'Authorization': `Bearer ${$env.API_KEY}` } }
34);
35
36return { json: data };

3.3 Dead Letter Queue

Ví dụ
1Khi retry hết mà vẫn fail → Save to "dead letter" cho manual review
2
3HTTP Request (3 retries)
4→ Still fails
5→ Google Sheets (log failed item + error details)
6→ Slack alert "Manual review needed"

Checkpoint

Exponential backoff nghĩa là gì? Dead Letter Queue dùng khi nào?

4

📋 Logging

TB5 min

4. Logging

4.1 Execution Log

n8n tự động log mọi execution:

  • Settings → Executions → View all past runs
  • Filter by: Success, Error, date range, workflow
  • Click execution → See data at each node

4.2 Custom Logging

Ví dụ
1Google Sheets "Automation Log":
2| Timestamp | Workflow | Status | Details | Duration |
3
4Log node (Set):
5- timestamp: {{ $now.format('yyyy-MM-dd HH:mm:ss') }}
6- workflow: {{ $workflow.name }}
7- status: "success" / "error"
8- details: {{ $json.message || 'OK' }}
9- duration: {{ $execution.id }}

4.3 Structured Logging Pattern

Ví dụ
1Every workflow:
21. Start → Log "Started"
32. Key steps → Log progress
43. Success → Log "Completed"
54. Error → Log "Failed" + error details
6
7Log to:
8- Google Sheets (simple, visual)
9- Airtable (structured, filterable)
10- Database (production, queryable)
11- File (local debugging)

Checkpoint

Structured logging pattern gồm những bước nào? Log nên lưu ở đâu cho production?

5

📡 Monitoring & Alerting

TB5 min

5. Monitoring & Alerting

5.1 Health Check Workflow

Ví dụ
1Schedule (every 5 min)
2→ HTTP Request (check API endpoints)
3→ IF (status !== 200)
4 → Slack #alerts: "🔴 API down: {url}"
5 → Email on-call
6→ ELSE
7 → Log "✅ All systems operational"

5.2 Error Rate Monitoring

Ví dụ
1Schedule (every hour)
2→ n8n API: Get executions (last hour)
3→ Code: Calculate error rate
4 - Total executions
5 - Failed executions
6 - Error rate %
7→ IF (error rate > 10%)
8 → Slack alert: "⚠️ Error rate {rate}% in last hour"
9→ Log metrics

5.3 SLA Monitoring

Ví dụ
1Track: "Are our automations running on time?"
2
3Schedule (every morning)
4→ Check: Did "Daily Report" workflow run successfully yesterday?
5→ Check: Did "Data Sync" workflow complete within 10 min?
6→ IF any SLA breached
7 → Alert team
8 → Log SLA breach

Checkpoint

Health Check Workflow chạy mỗi bao lâu? Khi API down, workflow làm gì?

6

🏭 Production Best Practices

TB5 min

6. Production Best Practices

6.1 Workflow Checklist

Ví dụ
1Pre-Deploy:
2□ Error handling on every HTTP/API node
3□ Retry logic for external calls
4□ Input validation
5□ Credentials stored properly (not hardcoded)
6□ Timeouts configured
7□ Logging at key steps
8□ Error workflow connected
9□ Tested with edge cases
10□ Documented (notes on nodes)
11
12Post-Deploy:
13□ Monitor first 24h
14□ Check error logs daily
15□ Review execution times weekly
16□ Update credentials before expiry
17□ Backup workflow JSON monthly

6.2 Naming Conventions

Ví dụ
1Workflows:
2 [Team] [Frequency] Description
3 e.g., "Sales | Daily | Revenue Report"
4 e.g., "HR | Webhook | New Employee Onboarding"
5
6Nodes:
7 Action + Target
8 e.g., "Get Sales Data", "Filter Active", "Send Slack Alert"
9
10Credentials:
11 Service + Environment
12 e.g., "Google Sheets - Production", "OpenAI - Dev"

6.3 Version Control

Ví dụ
1Export workflow JSON regularly:
2- Settings → Download → Save as .json
3- Commit to Git repository
4- Tag versions: v1.0, v1.1, etc.
5
6Benefits:
7- Rollback khi có lỗi
8- Code review cho workflow changes
9- Team collaboration

6.4 Security Practices

Ví dụ
1✅ Store secrets in Credentials, not workflow
2✅ Limit webhook access (auth, IP whitelist)
3✅ Use read-only API keys khi có thể
4✅ Rotate API keys quarterly
5✅ Audit who has n8n access
6✅ Enable 2FA cho n8n instance
7
8❌ Share API keys qua Slack/email
9❌ Use admin credentials cho automations
10❌ Skip error handling "because it works"

Checkpoint

Kể ra 5 items quan trọng nhất trong Pre-Deploy checklist cho production workflows.

7

🎓 Capstone Project

TB5 min

7. Capstone Project

7.1 Build: Automated Business Dashboard

Objective: Tạo complete automation system thu thập, xử lý, và report business data.

7.2 Requirements

#FeatureNodes Used
1Collect data từ 3 sourcesGoogle Sheets, HTTP Request, Airtable
2Clean & transform dataSet, Filter, Code
3Merge data sourcesMerge node
4Calculate KPIsCode node (aggregation)
5Generate reportCode (format), Set
6Distribute qua 2 channelsSlack + Email
7Error handlingError workflow, retry, logging
8Schedule tự độngSchedule Trigger

7.3 Architecture

Automated Business Dashboard

Schedule (Daily 8 AM)
📊Google Sheets (sales)
🌐HTTP Request (inventory API)
📋Airtable (customer data)
🔀Merge (Join by productId)
💻Code (Clean + Calculate KPIs)
💬Slack (#daily-report)
📧Email (managers)
📝Google Sheets (log)
🚨Error Handler → Slack (#alerts)

7.4 KPIs to Calculate

Ví dụ
1📈 Revenue: Total, by category, growth %
2📦 Inventory: Stock levels, low stock alerts
3👥 Customers: New, returning, churn rate
4📊 Top Products: By revenue, by quantity
5⚠️ Alerts: Low stock, high returns, unusual activity

7.5 Rubric

CriteriaPoints
Data collection (3 sources working)20
Data transformation (clean, merge, calculate)20
Report quality (formatted, readable)15
Multi-channel delivery (Slack + Email)15
Error handling (graceful, with alerts)15
Logging & monitoring10
Code quality (named nodes, notes)5
Total100

7.6 Extension Challenges

  • Interactive: Add Slack slash command /report weekly
  • Smart alerts: AI analyze trends, alert anomalies
  • Dashboard: Send data to Google Data Studio
  • Multi-timezone: Schedule reports cho teams ở các timezone khác nhau

📝 Course Summary

Đã học trong 12 bài:

ModuleBàiTopic
Fundamentals01n8n Overview
02Workflow Automation Concepts
03Interface & Basic Nodes
04Google Workspace Integration
Integrations05Trigger Types
06Notion & Airtable
07Slack & Discord
08HTTP Requests & APIs
Data09Expressions & Variables
10Data Transformation
11Code Node
Production12Error Handling & Monitoring

Skills Acquired

Ví dụ
1✅ Build automated workflows with n8n
2✅ Connect Google, Notion, Airtable, Slack, Discord
3✅ Use Schedule, Webhook, and App triggers
4✅ Call any API with HTTP Request node
5✅ Write expressions for dynamic data
6✅ Transform data (merge, filter, sort, aggregate)
7✅ Write custom JavaScript in Code node
8✅ Handle errors and monitor production workflows

🎯 What's Next?

Ví dụ
1📚 Recommended learning paths:
21. n8n Advanced → Sub-workflows, custom nodes, self-hosting
32. AI Automation → Connect GPT/Claude to n8n workflows
43. Database Integration → PostgreSQL, MongoDB, MySQL with n8n
54. DevOps Automation → CI/CD, deployment, monitoring
65. Business Process → CRM, billing, HR automation

Chúc mừng bạn đã hoàn thành khóa n8n Basics! 🎉

Checkpoint

Capstone Project yêu cầu bao nhiêu features? Mô tả architecture tổng quan của Automated Business Dashboard.