fix(retry): pass error to delay callback#1759
Conversation
The `delay` option now receives the error object as its second argument, matching the `shouldRetry` callback signature. This allows error-aware retry intervals (e.g., respecting Retry-After headers). Closes toss#1707
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Adds error-aware retry delays by extending the delay callback signature to receive the thrown error, with updated docs and tests to reflect the new behavior.
Changes:
- Extend
RetryOptions.delayfunction type to(attempts, error) => numberand pass the error when computing delay. - Add a unit test verifying the error is provided to the delay callback.
- Update English + localized docs with an error-based delay example and updated type signatures.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| src/function/retry.ts | Updates delay callback type and passes the caught error into delay computation; updates JSDoc. |
| src/function/retry.spec.ts | Adds test coverage for passing the error to delay. |
| docs/reference/function/retry.md | Documents error-aware delay usage and updates delay option signature. |
| docs/zh_hans/reference/function/retry.md | Same as above (Simplified Chinese). |
| docs/ko/reference/function/retry.md | Same as above (Korean). |
| docs/ja/reference/function/retry.md | Same as above (Japanese). |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| throw err; | ||
| } | ||
|
|
||
| const currentDelay = typeof delay === 'function' ? delay(attempts) : delay; | ||
| const currentDelay = typeof delay === 'function' ? delay(attempts, error) : delay; | ||
| await delayToolkit(currentDelay); |
| * @default 0 | ||
| * @example | ||
| * delay: (attempts) => attempt * 50 | ||
| * delay: (attempts, error) => error.status === 429 ? 10000 : attempt * 50 |
| @@ -90,6 +90,9 @@ export async function retry<T>(func: () => Promise<T>, retries: number): Promise | |||
| * // Retry a function with a delay increasing linearly by 50ms per attempt | |||
| * retry(() => fetchData(), { delay: (attempts) => attempt * 50, retries: 5 }); | |||
| delay: (_attempts, error) => { | ||
| if (error instanceof ResponseError && error.status === 429) { | ||
| return error.retryAfter * 1000; | ||
| } |
|
Thanks @copilot-pull-request-reviewer for the review.
|
- Fix `attempt` → `attempts` in JSDoc `@example` blocks - Replace `ResponseError` with self-contained type narrowing in all doc examples
Summary
Closes #1707. Enable error-aware retry intervals by passing the error object to the
delaycallback, consistent with howshouldRetryalready receives it.Changes
delaycallback now receives(attempts: number, error: unknown)— error is passed as the second argument alongside the attempt numberdelay?: number | ((attempts: number, error: unknown) => number)type updated in both theRetryOptionsinterface and implementationTest results
Full suite: 560 test files, 4482 tests passed, 0 failures.