Seven Failure Modes Every HTTP Client Should Handle: Lessons From Fixing 31 Bugs
Fixing 31 bugs in a TypeScript fetch client revealed seven failure modes every HTTP client should handle: retries, aborts, streams, SSR and XSS.
Most HTTP client bugs aren't on the happy path. They show up when a request is canceled, a stream drops halfway, or a server misbehaves. I recently audited every entry point of tanstack-fetch, an open-source typed fetch client for TanStack Query, and fixed 31 bugs in version 2.2.1. Almost none of them were in the main request flow.
Below are the seven patterns behind those bugs. They apply to any HTTP or streaming client you write or depend on.
1. Not Every Error Is a Network Error
A common design is to wrap any exception from inside the request pipeline as a "network error." The problem is that network errors are usually retryable.
In our case, if a response interceptor threw, for example, to redirect on a 401, the error became NETWORK_ERROR with status: 0, and the retry logic could run again. For a non-idempotent POST, that means a duplicate write.
The fix: Classify errors by where they come from. Errors raised by user code (interceptors, callbacks) are rethrown unchanged and never retried. Only transport failures are retryable.
2. Cancellation Must Be Terminal
controller.abort(reason) was going through the same path and also being retried. Cancellation is an explicit decision by the caller, and retrying it breaks that.
The fix: Check signal.aborted before treating something as a failure, and reject with signal.reason:
// 2.2.1 behaviour: rejects with your reason, never retried
controller.abort(new Error('user navigated away'))
A related issue: in environments without AbortSignal.any (Safari before 17.4, older Node versions), linking a caller's signal to an internal one leaked one listener per request. Always remove listeners in a finally block.
3. Validate Headers You Don't Control
Retry-After can be either a number of seconds or an HTTP date. Malformed values like -5 or soon 5 were parsed as 0, which meant retrying immediately against a server that was asking for less traffic.
The fix: Accept only non-negative integers and valid HTTP dates. Anything else falls back to the default backoff.
4. Stream Parsers Must Survive Arbitrary Chunk Boundaries
TCP gives you no guarantees about where chunks split. A server-sent-events parser that checks each chunk for \r\n will break when \r ends one chunk and \n starts the next:
chunk 1: "data: a\r"
chunk 2: "\ndata: b\r\n\r\n"
expected: one event with data "a\nb"
Other spec details that are easy to miss:
- An
id:-only block still updates the last event ID. - A bare
dataline adds an empty line. - Blocks with no data are not dispatched.
The fix: Keep a pending-CR state across chunks, and test the parser by splitting the same input at every possible byte offset.
5. "Resume" Must Cover the Case It Exists For
Our sse-resume plugin sent Last-Event-ID on reconnect, but only failures at connect time reached the reconnect path. Mid-stream drops, which are the main reason resume exists, were thrown straight to the caller.
The fix: Route every failure (non-OK status, network error, mid-stream drop) through one reconnect pipeline. Then decide what to retry by category:
- Network errors and 5xx: retry.
- 4xx: fail without retry.
Also reset the retry budget whenever an event arrives, so a long-lived stream doesn't run out of retries over its lifetime.
The same idea applies to WebSockets. A server that accepts a connection and then closes with 1011 will cause an infinite reconnect loop if you reset the attempt counter on every successful open. We now reset it only after the socket has stayed open for five seconds.
6. Server-Side Rendering Has No "Current User"
Two SSR bugs:
- Side effects during render: Vue/Nuxt composables opened SSE and WebSocket connections from an
immediatewatcher during SSR. Those connections had no client and never closed. Connect only in the browser, and reportstatus: 'disconnected'on the server. - Module-level state: A Svelte helper stored the client in a module variable. On a server, module scope is shared by every request, so one user's client, and possibly their auth headers, could be used for another user's request. Use per-request context on the server.
7. Treat Developer Tools as Untrusted Rendering Surfaces
The DevTools panel rendered method, path, and mock route name with innerHTML. The route name came from a response header, so any server the app talked to could inject script into the developer's page. Debug UIs are easy to forget during security review.
The fix: use textContent for every value, without exception.
The Cost of Robustness
All 31 fixes added about 260 bytes gzip to the core (3.71KB → 3.97KB). That's a small price for these guarantees: no duplicate POSTs, aborts that actually stop the request, streams that resume, and no state shared across SSR requests.
Takeaways
- Classify errors by where they come from before you decide whether to retry.
- Treat cancellation as final.
- Validate every header you don't control.
- Fuzz stream parsers across chunk boundaries.
- Use one reconnect pipeline for every failure, and reset budgets based on progress.
- Never keep per-user state at module scope on the server.
- Never use
innerHTMLwith data that has crossed the network.
The full list of fixes and behavior changes is in the 2.2.1 release notes.
Opinions expressed by DZone contributors are their own.