fix: do not call onResponse when no response was received - #89
Merged
Merged
Conversation
When fetch itself fails (e.g. network error like ECONNREFUSED), onResponse was called with undefined despite its typing guaranteeing a Response. Only invoke onResponse when a response actually exists; network failures still flow through onError. Co-authored-by: Laurent Blondy <L-Blondy@users.noreply.github.com>
Reword the onResponse doc comments and README entries, drop the inline comment in up.ts, and test against a real refused connection (127.0.0.1:59999) instead of an msw-simulated network error. Co-authored-by: Laurent Blondy <L-Blondy@users.noreply.github.com>
Hoisting the throw above the onResponse calls (instead of guarding with if (response)) establishes the invariant that no error implies a response was received, making the remaining non-null assertions sound. It also covers the retry case where an earlier attempt got a response but the final attempt failed: onResponse no longer fires with a stale response, and a throwing hook can no longer mask the original network error. Co-authored-by: Laurent Blondy <L-Blondy@users.noreply.github.com>
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.
Fixes #88
Problem
onResponseis typed(response: Response, request: Request)but was invoked withundefinedwhenfetchFnrejected (e.g.ECONNREFUSED), via an unsound non-null assertion inup.ts. A hook trusting the type then threw aTypeErrorthat masked the original network error.Repro (before this fix):
Fix
Throw the stored error before invoking
onResponse:error === undefinedimplies a response was received, so the remainingresponse!assertions become sound.onResponsenow fires iff the request concluded with a response; it still runs beforeonErrorfor rejected (non-2xx) responses.onResponseno longer fires with the stale response, and a throwing hook can no longer mask the original error.onError(and the error is still thrown), so no information is lost.This was chosen over widening the type to
Response | undefined, which would be a breaking change forcing every consumer to handleundefined.Also updates the
onResponsedoc comments and README tables to clarify that it only runs when a response is received.Testing
ECONNREFUSED) must not triggeronResponse(default or fetcher-level), and the call still rejects.onResponsemust not fire with the stale first response.biome check, andtsc --noEmit.