-
Notifications
You must be signed in to change notification settings - Fork 8
feat: add robust error handling for transient network failures #395
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nadeem-cs
wants to merge
6
commits into
development
Choose a base branch
from
enhancement/DX-3178
base: development
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
499c7ba
feat: add robust error handling for transient network failures
nadeem-cs fd1a567
fix: existing test cases for concurrency
nadeem-cs 9b4bce0
chore: Validate and sanitize URL before making request for SSRF preve…
nadeem-cs dd4661e
feat: adds jitter to networkRetryDelay
nadeem-cs d468866
fix: retryNetworkError method to update the running queue using shifts
nadeem-cs dbb940b
chore: improve sanitization on concurreny queue logic
nadeem-cs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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) => { | ||
nadeem-cs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// 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 | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.