fix(fetch): retry network errors regardless of retryStatusCodes#606
fix(fetch): retry network errors regardless of retryStatusCodes#606amitmishra11 wants to merge 1 commit into
Conversation
When a request fails at the network level (no HTTP response is returned), onError previously fell back to status code 500 to decide whether to retry. This caused a regression: callers who supplied a custom retryStatusCodes array that deliberately excluded 500 (e.g. to avoid re-sending data on server errors) also lost retries for pure connection failures that never produced any response. Fix: when context.response is absent, skip the status-code gate entirely and retry based solely on the remaining retry count. The retryStatusCodes option only makes sense when the server actually returned a response. Fixes unjs#495 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
📝 WalkthroughWalkthroughModified the retry condition in createFetch's onError handler so that requests without an HTTP response (e.g., network errors) are retried whenever retries remain, without matching against retryStatusCodes. Added a test verifying retries occur on network failures even when retryStatusCodes excludes 500. ChangesNetwork Error Retry Fix
Estimated code review effort: 1 (Trivial) | ~5 minutes Sequence Diagram(s)sequenceDiagram
participant Caller
participant ofetch as "$fetch"
participant onError
participant fetchImpl as "globalThis.fetch"
Caller->>ofetch: call with retry: 2, retryStatusCodes: [502,503,504]
ofetch->>fetchImpl: attempt request
fetchImpl-->>ofetch: throws error (no response)
ofetch->>onError: handle error
onError->>onError: retries > 0 and no context.response -> retry
onError->>fetchImpl: retry attempt
fetchImpl-->>ofetch: throws error (no response)
onError->>fetchImpl: retry attempt
fetchImpl-->>ofetch: throws error (no response)
ofetch-->>Caller: fails after 3 total attempts
Related Issues: Suggested labels: bug, retry Suggested reviewers: pi0 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
test/index.test.ts (1)
312-338: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low valueTest correctly validates the fix.
Covers the exact scenario from the PR: network error with
retryStatusCodesexcluding500still retries, and cleanup is handled viafinally.Optionally,
vi.spyOn(globalThis, "fetch")withmockImplementationwould be more idiomatic for vitest and integrates withvi.restoreAllMocks(), but the current manual save/restore pattern is functionally safe.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@test/index.test.ts` around lines 312 - 338, The test in the $fetch retry coverage works, but it manually replaces globalThis.fetch instead of using Vitest’s spying utilities. Update the network error test to use vi.spyOn(globalThis, "fetch") with a mock implementation, and rely on vi.restoreAllMocks() for cleanup so it matches the rest of the suite while preserving the same retry assertions.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@test/index.test.ts`:
- Around line 312-338: The test in the $fetch retry coverage works, but it
manually replaces globalThis.fetch instead of using Vitest’s spying utilities.
Update the network error test to use vi.spyOn(globalThis, "fetch") with a mock
implementation, and rely on vi.restoreAllMocks() for cleanup so it matches the
rest of the suite while preserving the same retry assertions.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 19e0a1d9-5144-4f44-9aa3-a2bb246afd29
📒 Files selected for processing (2)
src/fetch.tstest/index.test.ts
Bug
Closes #495.
When a request fails at the network level (no HTTP response is produced at all — e.g. a connection refused, DNS failure, or fetch abort via timeout),
onErrorfell back to status code500as a sentinel:Because the default
retryStatusCodesset includes500, this worked fine out of the box. But callers who provided a customretryStatusCodesarray that deliberately excluded500(a reasonable choice: re-sending a POST on a 500 can cause duplicate side-effects on the server) would unknowingly also stop retrying pure connection errors that never returned any response at all. The two failure modes are conflated by the|| 500fallback.Fix
When
context.responseis absent, skip the status-code gate entirely and allow the retry based solely on the remaining retry count. TheretryStatusCodesoption is only meaningful when the server actually returned an HTTP response.Reproduction
Test
A new test in
test/index.test.tsstubsglobalThis.fetchto throw aTypeError(simulating a network error), then asserts thatofetchstill performs the expected number of retry attempts whenretryStatusCodesexcludes500.Summary by CodeRabbit