Sort Failures Into Retry or Stop

Your batch script now prices every draft and holds back the ones that were cut off. It still assumes that every call succeeds.

On a busy morning, one won’t. ask() throws an error such as Error: 503: ..., and the loop over the inbox stops with half the emails still waiting for a draft. Should the script send that request again or stop?

The answer depends on the failure. Some go away if you wait a moment, and others come back on every attempt until you change something. In this lesson, you’ll sort model API failures into those two groups based on their status and error code, and you’ll make ask() attach that verdict to every error it throws for a failed response. Start with the part every failed response has, its status code.

Sort Failures by Status

When a call fails, the HTTP status returned by the provider already tells your code which group most failures belong to.

A failure is retryable when the same request, sent again a little later, can succeed. Here’s how the statuses you’ll meet from a model API sort into the two groups:

What happened Status What your code does
Too many requests or tokens for now 429 Retry after a wait
The server timed out or hit a conflict 408, 409 Retry after a wait
The provider’s servers failed or are overloaded 500 and above Retry after a wait
The request is malformed or too long 400 Stop and fix the request
The key is wrong or lacks access 401, 403 Stop the whole batch and fix the key
The model ID doesn’t exist for you 404 Stop and fix the ID
No response arrived at all none Stop and report the message

A 429 means you’ve hit a rate limit, a cap on how much you can send in a short time. Model APIs count tokens per minute as well as requests, so a few long emails can get a 429 after only a handful of calls. When the response carries a Retry-After header, it says how many seconds to wait or the date and time to retry. When it doesn’t, wait a little longer after each attempt, and give up after a few.

A status of 500 or above means the problem is on the provider’s side. A later attempt may succeed, but an outage can also outlast every attempt you make. So retries always have a cap, and when the last one fails, the script reports it and leaves that email undrafted.

The other 4xx statuses come from your request or your key, so the same request gets the same answer. The exceptions are 408 and 409: the server ran out of time or hit a conflict, which a second try can get past. A 401 fails for every email, so it stops the whole batch.

The last row has no status at all. When the connection drops, fetch() rejects with a TypeError whose message is fetch failed. When the adapter’s 60-second timeout runs out, fetch() rejects with a TimeoutError. Both happen before any response exists. A second try might work, but the error doesn’t say, so this course keeps the rule strict and retries only what the provider’s answer marks as temporary.

Read the Error Code, Too

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.