-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathrobust-error-handling.js
More file actions
87 lines (77 loc) · 3.25 KB
/
robust-error-handling.js
File metadata and controls
87 lines (77 loc) · 3.25 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
// Example: Configuring Robust Error Handling for Transient Network Failures
// This example shows how to use the enhanced retry mechanisms in the Contentstack Management SDK
const contentstack = require('../lib/contentstack')
// Example 1: Basic configuration with enhanced network retry
const clientWithBasicRetry = contentstack.client({
api_key: 'your_api_key',
management_token: 'your_management_token',
// Enhanced network retry configuration
retryOnNetworkFailure: true, // Enable network failure retries
maxNetworkRetries: 3, // Max 3 attempts for network failures
networkRetryDelay: 100, // Start with 100ms delay
networkBackoffStrategy: 'exponential' // Use exponential backoff (100ms, 200ms, 400ms)
})
// Example 2: Advanced configuration with fine-grained control
const clientWithAdvancedRetry = contentstack.client({
api_key: 'your_api_key',
management_token: 'your_management_token',
// Network failure retry settings
retryOnNetworkFailure: true,
retryOnDnsFailure: true, // Retry on DNS resolution failures (EAI_AGAIN)
retryOnSocketFailure: true, // Retry on socket errors (ECONNRESET, ETIMEDOUT, etc.)
retryOnHttpServerError: true, // Retry on HTTP 5xx errors
maxNetworkRetries: 5, // Allow up to 5 network retries
networkRetryDelay: 200, // Start with 200ms delay
networkBackoffStrategy: 'exponential',
// Original retry settings (for non-network errors)
retryOnError: true,
retryLimit: 3,
retryDelay: 500,
// Custom logging
logHandler: (level, message) => {
console.log(`[${level.toUpperCase()}] ${new Date().toISOString()}: ${message}`)
}
})
// Example 3: Conservative configuration for production
const clientForProduction = contentstack.client({
api_key: 'your_api_key',
management_token: 'your_management_token',
// Conservative retry settings for production
retryOnNetworkFailure: true,
maxNetworkRetries: 2, // Only 2 retries to avoid long delays
networkRetryDelay: 300, // Longer initial delay
networkBackoffStrategy: 'fixed', // Fixed delay instead of exponential
// Custom retry condition for additional control
retryCondition: (error) => {
// Custom logic: only retry on specific conditions
return error.response && error.response.status >= 500
}
})
// Example usage with error handling
async function demonstrateRobustErrorHandling () {
try {
const stack = clientWithAdvancedRetry.stack('your_stack_api_key')
const contentTypes = await stack.contentType().query().find()
console.log('Content types retrieved successfully:', contentTypes.items.length)
} catch (error) {
if (error.retryAttempts) {
console.error(`Request failed after ${error.retryAttempts} retry attempts:`, error.message)
console.error('Original error:', error.originalError?.code)
} else {
console.error('Request failed:', error.message)
}
}
}
// The SDK will now automatically handle:
// ✅ DNS resolution failures (EAI_AGAIN)
// ✅ Socket errors (ECONNRESET, ETIMEDOUT, ECONNREFUSED)
// ✅ HTTP timeouts (ECONNABORTED)
// ✅ HTTP 5xx server errors (500-599)
// ✅ Exponential backoff with configurable delays
// ✅ Clear logging and user-friendly error messages
module.exports = {
clientWithBasicRetry,
clientWithAdvancedRetry,
clientForProduction,
demonstrateRobustErrorHandling
}