DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Migrating From the OpenAI API to vLLM: Configure It Right
  • From Microservices to Agent Services: The Next Architectural Shift
  • Why Your "Stateless" Services Are Lying to You
  • MCP Elicitation: Human-in-the-Loop for MCP Servers

Trending

  • Relate: A SQLite-Style Graph Database
  • From Theory to Practice: Building an Enterprise Ontology With Python and Kubernetes
  • How to Format Articles for DZone
  • Why Successful LLM Demos Fail in Production

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. Tools
  4. Seven Failure Modes Every HTTP Client Should Handle: Lessons From Fixing 31 Bugs

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.

By 
Mohammad Garmabi
·
Dec. 03, 26 · Analysis
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
104 Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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:

TypeScript
 
// 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:

Plain Text
 
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 data line 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 immediate watcher during SSR. Those connections had no client and never closed. Connect only in the browser, and report status: '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

  1. Classify errors by where they come from before you decide whether to retry.
  2. Treat cancellation as final.
  3. Validate every header you don't control.
  4. Fuzz stream parsers across chunk boundaries.
  5. Use one reconnect pipeline for every failure, and reset budgets based on progress.
  6. Never keep per-user state at module scope on the server.
  7. Never use innerHTML with data that has crossed the network.

The full list of fixes and behavior changes is in the 2.2.1 release notes.

Transmission Control Protocol Requests Tool

Opinions expressed by DZone contributors are their own.

Related

  • Migrating From the OpenAI API to vLLM: Configure It Right
  • From Microservices to Agent Services: The Next Architectural Shift
  • Why Your "Stateless" Services Are Lying to You
  • MCP Elicitation: Human-in-the-Loop for MCP Servers

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook