1
+ // Example: Configuring Robust Error Handling for Transient Network Failures
2
+ // This example shows how to use the enhanced retry mechanisms in the Contentstack Management SDK
3
+
4
+ const contentstack = require ( '../lib/contentstack' )
5
+
6
+ // Example 1: Basic configuration with enhanced network retry
7
+ const clientWithBasicRetry = contentstack . client ( {
8
+ api_key : 'your_api_key' ,
9
+ management_token : 'your_management_token' ,
10
+ // Enhanced network retry configuration
11
+ retryOnNetworkFailure : true , // Enable network failure retries
12
+ maxNetworkRetries : 3 , // Max 3 attempts for network failures
13
+ networkRetryDelay : 100 , // Start with 100ms delay
14
+ networkBackoffStrategy : 'exponential' // Use exponential backoff (100ms, 200ms, 400ms)
15
+ } )
16
+
17
+ // Example 2: Advanced configuration with fine-grained control
18
+ const clientWithAdvancedRetry = contentstack . client ( {
19
+ api_key : 'your_api_key' ,
20
+ management_token : 'your_management_token' ,
21
+ // Network failure retry settings
22
+ retryOnNetworkFailure : true ,
23
+ retryOnDnsFailure : true , // Retry on DNS resolution failures (EAI_AGAIN)
24
+ retryOnSocketFailure : true , // Retry on socket errors (ECONNRESET, ETIMEDOUT, etc.)
25
+ retryOnHttpServerError : true , // Retry on HTTP 5xx errors
26
+ maxNetworkRetries : 5 , // Allow up to 5 network retries
27
+ networkRetryDelay : 200 , // Start with 200ms delay
28
+ networkBackoffStrategy : 'exponential' ,
29
+
30
+ // Original retry settings (for non-network errors)
31
+ retryOnError : true ,
32
+ retryLimit : 3 ,
33
+ retryDelay : 500 ,
34
+
35
+ // Custom logging
36
+ logHandler : ( level , message ) => {
37
+ console . log ( `[${ level . toUpperCase ( ) } ] ${ new Date ( ) . toISOString ( ) } : ${ message } ` )
38
+ }
39
+ } )
40
+
41
+ // Example 3: Conservative configuration for production
42
+ const clientForProduction = contentstack . client ( {
43
+ api_key : 'your_api_key' ,
44
+ management_token : 'your_management_token' ,
45
+ // Conservative retry settings for production
46
+ retryOnNetworkFailure : true ,
47
+ maxNetworkRetries : 2 , // Only 2 retries to avoid long delays
48
+ networkRetryDelay : 300 , // Longer initial delay
49
+ networkBackoffStrategy : 'fixed' , // Fixed delay instead of exponential
50
+
51
+ // Custom retry condition for additional control
52
+ retryCondition : ( error ) => {
53
+ // Custom logic: only retry on specific conditions
54
+ return error . response && error . response . status >= 500
55
+ }
56
+ } )
57
+
58
+ // Example usage with error handling
59
+ async function demonstrateRobustErrorHandling ( ) {
60
+ try {
61
+ const stack = clientWithAdvancedRetry . stack ( 'your_stack_api_key' )
62
+ const contentTypes = await stack . contentType ( ) . query ( ) . find ( )
63
+ console . log ( 'Content types retrieved successfully:' , contentTypes . items . length )
64
+ } catch ( error ) {
65
+ if ( error . retryAttempts ) {
66
+ console . error ( `Request failed after ${ error . retryAttempts } retry attempts:` , error . message )
67
+ console . error ( 'Original error:' , error . originalError ?. code )
68
+ } else {
69
+ console . error ( 'Request failed:' , error . message )
70
+ }
71
+ }
72
+ }
73
+
74
+ // The SDK will now automatically handle:
75
+ // ✅ DNS resolution failures (EAI_AGAIN)
76
+ // ✅ Socket errors (ECONNRESET, ETIMEDOUT, ECONNREFUSED)
77
+ // ✅ HTTP timeouts (ECONNABORTED)
78
+ // ✅ HTTP 5xx server errors (500-599)
79
+ // ✅ Exponential backoff with configurable delays
80
+ // ✅ Clear logging and user-friendly error messages
81
+
82
+ module . exports = {
83
+ clientWithBasicRetry,
84
+ clientWithAdvancedRetry,
85
+ clientForProduction,
86
+ demonstrateRobustErrorHandling
87
+ }
0 commit comments