Skip to content

Extract initial fetch error detection into reusable utility#4987

Closed
Copilot wants to merge 3 commits intomainfrom
copilot/extract-error-handling-utility
Closed

Extract initial fetch error detection into reusable utility#4987
Copilot wants to merge 3 commits intomainfrom
copilot/extract-error-handling-utility

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 5, 2026

makeChatRequest2 provides no direct way to detect fetch-level errors (e.g. HTTP 404) before the stream begins. xtabProvider worked around this by manually wiring a DeferredPromise<void> + Promise.race() — boilerplate that's hard to follow and easy to get wrong.

Changes

  • New createInitialFetchErrorDetector() in src/platform/chat/common/chatMLFetcher.ts
    Encapsulates the race-against-first-token pattern behind a clean, composable API:

    const { wrapFinishedCb, getInitialFetchError } = createInitialFetchErrorDetector();
    
    const fetchResultPromise = endpoint.makeChatRequest2({
      ...,
      finishedCb: wrapFinishedCb(myFinishedCb), // transparently signals first-token receipt
    }, token);
    
    const initialError = await getInitialFetchError(fetchResultPromise);
    if (initialError) {
      // fetch failed before any tokens arrived (e.g. 404, rate limit)
    }
    • wrapFinishedCb(cb) — decorates any FinishedCallback to signal when the first token arrives, passing all arguments through unchanged
    • getInitialFetchError(fetchResultPromise) — races the first-token signal against the full fetch result; returns the ChatFetchError if the fetch settles before any tokens, undefined otherwise
  • src/extension/xtab/node/xtabProvider.ts
    Replaced the manual DeferredPromise<void> + inline Promise.race + firstTokenReceived.complete() in .finally with a single createInitialFetchErrorDetector() call. The resulting variable names (initialFetchError vs fetchRes) 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, undefined callback, and multiple error response types.

Original prompt

makeChatRequest2's API doesn't allow to get if the initial fetch errors (to handle 404) before we can get the stream.

Currently, this's handled by racing between firs token received and fetch result promise. However, I would prefer if we could extract this to a utility function with a nicer API.

Employ excellent engineering approach.

The user has attached the following file paths as relevant context:

  • .github/copilot-instructions.md

Created from VS Code.

Copilot AI and others added 2 commits April 5, 2026 20:15
…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
Copilot AI requested a review from ulugbekna April 5, 2026 21:12
@ulugbekna ulugbekna closed this Apr 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants