Skip to content

feat(runtime): structure remote fetch errors - #28

Merged
gioboa merged 4 commits into
module-federation:mainfrom
dmchoi77:remote-fetch-errors
Nov 23, 2026
Merged

gioboa merged 4 commits into
module-federation:mainfrom
dmchoi77:remote-fetch-errors

Conversation

@dmchoi77

Copy link
Copy Markdown
Contributor

Summary

Problem

Remote fetch failures are currently exposed mostly as generic Error instances. A person can infer the cause by reading the message, but consumers have no stable fields for distinguishing network, timeout, HTTP, response-size, and JSON parse failures. Automated decisions such as retries therefore have to be reimplemented by each caller.

Before and after

Before After
Error type Mostly generic Error RemoteFetchError
Classification Inspect message and cause Stable code
HTTP details Partially encoded in the message Preserve status, statusText, and bodyPreview
Retry decisions Caller interprets the error Read retryable directly
Message role Human-readable information and the de facto discriminator Logging and debugging information

This PR keeps messages and the original cause, while separating human-readable diagnostics from structured classification fields.

Changes

  • Add RemoteFetchError and TANSTACK-REMOTE-FETCH-* error codes
  • Classify network, timeout, HTTP, response-size, and JSON parse failures
  • Expose HTTP status, statusText, a maximum 240-character bodyPreview, and retryable
  • Preserve the original timeout cause at the Node entry boundary and classify HTTP preview timeouts correctly
  • Update runtime exports, README, npm release artifact contracts, and tests

Validation

  • pnpm --filter @module-federation/tanstack typecheck: passed
  • node --test test/remote-fetch.test.mjs: 7/7 passed
  • pnpm --filter @module-federation/tanstack build: passed
  • node --test test/release-contracts.test.mjs: 32/32 passed
  • node --test test/release-artifacts.test.mjs: 2/2 passed

@pkg-pr-new

pkg-pr-new Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/module-federation/tanstack/@module-federation/tanstack@28

commit: 4070a52

@dmchoi77
dmchoi77 marked this pull request as draft November 23, 2026 12:29
@dmchoi77
dmchoi77 force-pushed the remote-fetch-errors branch from f04e9ce to 2d9dbc0 Compare November 23, 2026 12:35
@dmchoi77
dmchoi77 marked this pull request as ready for review November 23, 2026 12:40
@Nsttt

Nsttt commented Nov 23, 2026

Copy link
Copy Markdown
Member

Thanks for this. Typed fetch errors with a stable code and a retryable flag are a real improvement over parsing messages. Two classification bugs undercut that promise, though, so I'd like those fixed before merging. The rest are smaller follow-ups.

Must fix

1. A stalled body on an HTTP error turns a known status into a timeout, or a hang. remote-fetch.ts#L88-L100

Previously fetchRemoteText threw as soon as it saw !response.ok. It now awaits readBodyPreview(response) first, under the same overall timeout. If a remote sends 404 headers and then holds the body open:

  • With the default 10s timeout, the caller waits 10s and gets TANSTACK-REMOTE-FETCH-TIMEOUT with retryable: true and no status. The 404 was already known and isn't retryable.
  • With fetchTimeoutMs: 0 there's no controller, so the promise never settles. Before this PR it rejected immediately.

The test fetchRemoteText classifies a timeout while reading an HTTP error preview asserts the timeout result, so it currently locks this behavior in. Suggested fix: give the preview read its own short deadline (or race it against a small timer) and always throw the HTTP error once the status is known. Attach bodyPreview only if it arrived in time.

2. Vite-path fetch failures get the wrong code and retryable. node-entry-loader.ts#L58-L64

When phase === "fetch" and no code is passed, RemoteEntryError defaults to REMOTE_FETCH_NETWORK, and retryable defaults to false. Neither Vite call site passes a code or retryable: the httpError branch (L356-L366) and the unreachable-HEAD branch (L370-L384). As a result:

  • A Vite remote returning 503 is reported as NETWORK and non-retryable.
  • A Vite remote that's down is non-retryable, while the same outage on an Rsbuild/CJS remote is retryable: true.

Suggested fix: pass an explicit code/retryable at both sites. Use HTTP plus isRetryableHttpStatus when the httpError exposes a status, and NETWORK with retryable: true for the unreachable case. Alternatively, default retryable to true whenever the code falls back to NETWORK.

Smaller follow-ups

  • RemoteEntryError.cause has a different shape per failure kind (L193). cause.cause ?? cause unwraps timeouts and network errors to the raw DOMException/TypeError, but HTTP and size errors stay wrapped as RemoteFetchError. So err.cause.status and err.cause instanceof RemoteFetchError only work for some failures. I'd always keep the RemoteFetchError as cause; its own cause still carries the AbortError.
  • Network errors have no URL in the message (remote-fetch.ts#L80, and the outer catch too). Logs just show fetch failed. Something like ```Could not reach ${url}: ${errorMessage(cause)}` `` would fix it. Also, the timeout message says "did not respond" even when headers arrived and only the body stalled. "Did not complete within" would be accurate in both cases.
  • Public API nothing uses (remote-errors.ts). RemoteErrorReporter/RemoteErrorEvent are exported from runtime, but no option accepts a reporter, reportRemoteError is never called, and the serverFunction* codes are never thrown. I'd drop them until there's a consumer, or wire the reporter through getRemoteStylesheets and the node entry loader in this PR.
  • Behavior change worth flagging. Timeouts from getRemoteStylesheets used to reject with name === "AbortError" and now reject as RemoteFetchError (see the updated test/runtime.test.mjs). The README documents the new type but doesn't say the old shape changed. A BREAKING CHANGE: footer (or feat!) would get release-please to surface it, or at least add a line in the README/changelog.
  • The body preview can come out much shorter than 240 chars (remote-fetch.ts#L214). Reading stops at 240 raw characters and whitespace is collapsed afterwards, so an indented HTML error page can yield an almost empty preview. Separately, .slice(0, 240) can split a surrogate pair. Fix: keep reading, with a byte cap, until the normalized text reaches the limit, and slice by code points.

Happy to help with any of these if useful.

@gioboa gioboa left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keep the known HTTP status when an error body stalls instead of reporting a timeout or hanging, and classify Vite remote fetch failures as HTTP, timeout, or network with correct retryability.

@gioboa gioboa left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @dmchoi77 👏

@gioboa
gioboa merged commit 9abd29d into module-federation:main Nov 23, 2026
6 checks passed
@dmchoi77

Copy link
Copy Markdown
Contributor Author

@Nsttt Thank you for taking the time to review this so thoroughly. Your feedback was very helpful!

@gioboa Oh, I see you’ve already taken care of the fixes. Thank you so much for addressing them!

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.

3 participants