fix(fetch): clone request on retry to support requests with body - #79
Open
lucasros98 wants to merge 1 commit into
Open
fix(fetch): clone request on retry to support requests with body#79lucasros98 wants to merge 1 commit into
lucasros98 wants to merge 1 commit into
Conversation
Closes vippsas#78. The fetchRetry function was passing the same Request object to fetchJSON on every retry attempt. Once a Request body has been consumed by a fetch call, the same Request object cannot be used again — the runtime throws 'TypeError: Cannot construct a Request with a Request object that has already been used'. This affected any POST/PUT request with a body when the first attempt returned a retryable status (5xx). GET requests without a body were unaffected, which is why the existing retry tests did not catch it. Fix: call request.clone() before each fetch attempt so the original Request remains unconsumed and the next retry can construct a fresh fetch from it. Added a regression test that performs a POST with a JSON body, fails once with 500, and then succeeds on retry. The test fails on master with the exact stack trace from vippsas#78 and passes with this change.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #78.
fetchRetrywas passing the sameRequestobject tofetchJSONon every retry attempt. Once aRequestbody has been consumed by afetchcall, the sameRequestobject cannot be used again — the runtime throws:This affected any POST/PUT request with a body when the first attempt returned a retryable status (5xx). GET requests without a body were unaffected, which is why the existing retry tests did not catch it.
Fix
Call `request.clone()` before each fetch attempt so the original `Request` remains unconsumed and the next retry can construct a fresh fetch from it.
Test plan
Added a regression test (`fetchRetry - should retry POST request with body without consuming it`) that:
This test fails on `main` with the exact stack trace from #78:
…and passes with this change. All existing tests continue to pass (89 total).