/**
* Wraps a function to handle request headers with retry logic and error wrapping.
* @param {function} fn The function to wrap. Should return a promise.
* @param {object} options Configuration options.
* @param {number} options.retries Number of retry attempts.
* @param {number} options.retryDelayMs Delay between retries in milliseconds.
* @returns {function} The wrapped function.
*/
function withRetryAndErrorWrap(fn, options = {}) {
const { retries = 3, retryDelayMs = 1000 } = options;
return async function (...args) {
for (let i = 0; i < retries; i++) {
try {
const result = await fn(...args);
return result; // Success, return the result
} catch (error) {
// Handle the error
console.error(`Request header error (attempt ${i + 1}):`, error);
if (i === retries - 1) {
// Last attempt, re-throw the error
throw error;
}
// Wait before retrying
await new Promise(resolve => setTimeout(resolve, retryDelayMs));
}
}
// Should not reach here, but included for completeness
throw new Error("Max retries reached. Request header error persists.");
};
}
Add your comment