* Logs a message for a retry attempt when using exponential backoff. * @param attempt The current attempt number. * @param error The error that caused the retry. * @param errorStatus The HTTP status code of the error, if available.
( attempt: number, error: unknown, errorStatus?: number, )
| 306 | * @param errorStatus The HTTP status code of the error, if available. |
| 307 | */ |
| 308 | function logRetryAttempt( |
| 309 | attempt: number, |
| 310 | error: unknown, |
| 311 | errorStatus?: number, |
| 312 | ): void { |
| 313 | let message = `Attempt ${attempt} failed. Retrying with backoff...`; |
| 314 | if (errorStatus) { |
| 315 | message = `Attempt ${attempt} failed with status ${errorStatus}. Retrying with backoff...`; |
| 316 | } |
| 317 | |
| 318 | if (errorStatus === 429) { |
| 319 | console.warn(message, error); |
| 320 | } else if (errorStatus && errorStatus >= 500 && errorStatus < 600) { |
| 321 | console.error(message, error); |
| 322 | } else if (error instanceof Error) { |
| 323 | // Fallback for errors that might not have a status but have a message |
| 324 | if (error.message.includes('429')) { |
| 325 | console.warn( |
| 326 | `Attempt ${attempt} failed with 429 error (no Retry-After header). Retrying with backoff...`, |
| 327 | error, |
| 328 | ); |
| 329 | } else if (error.message.match(/5\d{2}/)) { |
| 330 | console.error( |
| 331 | `Attempt ${attempt} failed with 5xx error. Retrying with backoff...`, |
| 332 | error, |
| 333 | ); |
| 334 | } else { |
| 335 | console.warn(message, error); // Default to warn for other errors |
| 336 | } |
| 337 | } else { |
| 338 | console.warn(message, error); // Default to warn if error type is unknown |
| 339 | } |
| 340 | } |