1. /**
  2. * Wraps a function to handle request headers with retry logic and error wrapping.
  3. * @param {function} fn The function to wrap. Should return a promise.
  4. * @param {object} options Configuration options.
  5. * @param {number} options.retries Number of retry attempts.
  6. * @param {number} options.retryDelayMs Delay between retries in milliseconds.
  7. * @returns {function} The wrapped function.
  8. */
  9. function withRetryAndErrorWrap(fn, options = {}) {
  10. const { retries = 3, retryDelayMs = 1000 } = options;
  11. return async function (...args) {
  12. for (let i = 0; i < retries; i++) {
  13. try {
  14. const result = await fn(...args);
  15. return result; // Success, return the result
  16. } catch (error) {
  17. // Handle the error
  18. console.error(`Request header error (attempt ${i + 1}):`, error);
  19. if (i === retries - 1) {
  20. // Last attempt, re-throw the error
  21. throw error;
  22. }
  23. // Wait before retrying
  24. await new Promise(resolve => setTimeout(resolve, retryDelayMs));
  25. }
  26. }
  27. // Should not reach here, but included for completeness
  28. throw new Error("Max retries reached. Request header error persists.");
  29. };
  30. }

Add your comment