Extract initial fetch error detection into reusable utility#4987
Closed
Extract initial fetch error detection into reusable utility#4987
Conversation
…eInitialFetchErrorDetector Agent-Logs-Url: https://github.com/microsoft/vscode-copilot-chat/sessions/7d5eb4b8-a683-4d39-93c6-a986bf34d81f Co-authored-by: ulugbekna <16353531+ulugbekna@users.noreply.github.com>
…DeferredPromise lifecycle doc Agent-Logs-Url: https://github.com/microsoft/vscode-copilot-chat/sessions/7d5eb4b8-a683-4d39-93c6-a986bf34d81f Co-authored-by: ulugbekna <16353531+ulugbekna@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Extract error handling to utility function for API calls
Extract initial fetch error detection into reusable utility
Apr 5, 2026
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.
makeChatRequest2provides no direct way to detect fetch-level errors (e.g. HTTP 404) before the stream begins.xtabProviderworked around this by manually wiring aDeferredPromise<void>+Promise.race()— boilerplate that's hard to follow and easy to get wrong.Changes
New
createInitialFetchErrorDetector()insrc/platform/chat/common/chatMLFetcher.tsEncapsulates the race-against-first-token pattern behind a clean, composable API:
wrapFinishedCb(cb)— decorates anyFinishedCallbackto signal when the first token arrives, passing all arguments through unchangedgetInitialFetchError(fetchResultPromise)— races the first-token signal against the full fetch result; returns theChatFetchErrorif the fetch settles before any tokens,undefinedotherwisesrc/extension/xtab/node/xtabProvider.tsReplaced the manual
DeferredPromise<void>+ inlinePromise.race+firstTokenReceived.complete()in.finallywith a singlecreateInitialFetchErrorDetector()call. The resulting variable names (initialFetchErrorvsfetchRes) are also more self-documenting.src/platform/chat/test/common/chatMLFetcher.spec.ts(new)6 unit tests covering: first-token wins the race, fetch error before any tokens, fetch success before tokens, original callback delegation,
undefinedcallback, and multiple error response types.Original prompt
Created from VS Code.