I'm Eric Gitangu. I work on backend and platform systems for a mobility/fintech platform. This one is about what the user sees when the server says "slow down".
The symptom
An internal admin web app talks to the backend through one shared client. That client already had a rule for HTTP 429: tell the user to wait, say for how long when the server said, and never retry on its own.
Then I looked at what a rate-limited reply actually did across the whole app. Most screens go through the shared client. Plenty do not: typed readers for one feature, an upload helper, an operations client with its own error class, an identity resolver with its own error class. Each one read the reply itself and built its own error text.
Even the shared client had a gap. It parsed the body as JSON first and threw a parse error if that failed. A 429 can come from the proxy in front of the backend, and that reply can be an HTML page. So the user saw "Bad JSON" where they should have seen "wait".
Nothing crashed. The message was just different on every screen, and some were not about waiting at all.
The decision
Three options:
- Fix the shared client only. Cheap, and every reader that bypasses it stays as it was.
- Route every call through one new client. Right in the long run, but it means rewriting readers that have their own typed errors and their own tests.
- One small rail any reader can call, plus a test that makes calling it mandatory.
I took the third. It changes each reader by a line or two, and the typed errors stay typed.
The rail
Two helpers:
limitError(response, body)returns an error that says to wait, or null when the reply is not limited.throwIfLimited(response)is for a reader that has not read the body yet. It reads a clone, so the caller can still read the original. An HTML body just means the status decides.
A reply counts as limited when the status is 429, or when the body carries one of the backend's rate-limit codes. The wait comes from the body first, then from the Retry-After header (seconds or an HTTP date), and is capped at a sane ceiling so a wild value can never lock someone out for good. The client never retries. The caller decides.
Typed readers keep their types. The operations client still throws its own error class and the identity resolver still throws its own. They now carry the same sentence, so the tests assert one string for all of them, with the wait the server gave. One test also checks the request ran once.
const err = limitError(res, body); // null when not limited
if (err) throw err; // "Too many requests. Try again in 20 seconds."The fence
So a source-level contract test walks the files and finds every call to fetch or to the shared transport wrappers. Each call must have one of the rail helpers in the same function, or a comment on its line or the three above: limit-exempt: <reason>. The reason needs real words. A bare marker fails the test.
The exemptions I wrote: a session probe where only a 401 matters, the shared transport itself (its callers are checked instead), and calls to third-party hosts.
Why the client doesn't retry
I left retry out on purpose. A silent retry on a rate-limited call spends the very budget the server is protecting, and the user never learns why the screen is slow. Saying "wait 20 seconds" and handing the decision back is cheaper. Callers that want a retry can add one, because the error carries the number.
What the tests pin down
- A typed read and an upload both throw the same sentence, with the wait the server gave.
- An HTML 429 with only a
Retry-Afterheader says the same thing. - A reply that is not limited is untouched, and its body can still be read by the caller.
- The request ran once.
What it costs
It is a grep, not a type system. It looks at a window of lines up to the next declaration, so a determined reader can pass without handling anything, and an exemption comment can outlive its reason. I accept that. It catches the common failure, a new reader written by someone who never heard of the rail, and review catches the rest. It also makes every new network call cost one extra line. I think that is the right price.
Status
Built, with tests, on a branch. Not merged, not live.
Copy this
The rail, the scan and one test file you can run are in a small gist. Names and numbers are made up: https://gist.github.com/ericgitangu/c6f92f2c257d2e2d422e68f124545bfd
The takeaway
I added a test that fails the build when a network call skips the 429 handling.
How do your clients make sure a "slow down" reads the same on every screen?