Sub topic 8
Retry, Fallback, and Partial Failure
Assume you already understand components, asynchronous data fetching, and basic HTML and TypeScript. This chapter focuses on what happens after a request fails, especially when the user can still make progress.
A failed request does not always mean the whole screen must fail. Design a recovery path that matches the cause and the amount of useful content that remains.
Choose a recovery path
A recovery path tells the user what happened and gives them a reasonable next action. Common paths include:
- Retry: Try the same request again. Use this for temporary network problems, timeouts, or overloaded services.
- Fallback: Show another source or a reduced version of the content. Use this when the primary source is unavailable but older or cached data is still useful.
- Partial success: Keep successful sections visible and isolate the failed section. Use this when a screen loads several independent resources.
- User correction: Ask the user to fix an input or permission problem. Retrying without a change will not help.
- Escalation: Provide a support or contact path for an account, billing, or server problem that the user cannot resolve.
Do not show a generic “Something went wrong” message for every cause. The message and action should explain whether trying again can change the result.
| Failure cause | Useful response | Avoid |
|---|---|---|
| Temporary network timeout | Retry and preserve entered work | Clearing the form |
| Expired session | Ask the user to sign in again | Repeating the request silently |
| Invalid input | Identify the field and correction | Offering retry only |
| Missing permission | Explain the required access | Pretending the data is empty |
| One widget service failed | Keep other widgets visible | Replacing the whole page with an error |
Retrying safely
A retry should be visible, predictable, and safe. Preserve the user's context, such as filters, scroll position, and unsaved text. Disable the retry button while its request is running so one click does not create several requests.
Some operations can be repeated without changing the result. Reading a list is usually safe to retry. Creating an order or charging a card may not be safe unless the server supports an idempotency key, which is a unique request identifier that lets the server recognize a repeated request.
Use automatic retries sparingly. A short retry for a temporary network interruption can help, but repeated attempts can waste resources and delay a useful error message. Exponential backoff increases the wait between attempts.
type RequestResult<T> =
| { kind: "ok"; value: T }
| { kind: "error"; message: string; retryable: boolean };
async function loadProjects(): Promise<RequestResult<string[]>> {
try {
const response = await fetch("/api/projects");
if (response.status === 401) {
return {
kind: "error",
message: "Your session has expired. Sign in again.",
retryable: false,
};
}
if (!response.ok) {
return {
kind: "error",
message: "Projects could not be loaded.",
retryable: response.status >= 500,
};
}
return { kind: "ok", value: await response.json() as string[] };
} catch {
return {
kind: "error",
message: "Check your connection and try again.",
retryable: true,
};
}
}
async function loadWithRetry(
attempts: number,
): Promise<RequestResult<string[]>> {
for (let attempt = 0; attempt < attempts; attempt += 1) {
const result = await loadProjects();
if (result.kind === "ok" || !result.retryable) {
return result;
}
await new Promise((resolve) => {
setTimeout(resolve, 250 * 2 ** attempt);
});
}
return {
kind: "error",
message: "Projects are still unavailable. Try again later.",
retryable: true,
};
}Fallback content
Fallback content should be clearly labeled. A cached timestamp such as “Showing data from 10 minutes ago” helps users judge whether it is safe to rely on. Do not present stale data as current.
A fallback can also be a reduced experience. For example, display a saved document while disabling live collaboration. Explain which feature is unavailable and leave the available content usable.
Handling partial failure
Treat independent resources independently. A dashboard might request account details, recent activity, and recommendations. If recommendations fail, the account details should not disappear.
Give each failed section a local message and action. Keep the page structure stable when possible, because replacing the entire layout makes the screen harder to scan and can move keyboard focus unexpectedly.
Review the recovery experience
Test more than the error banner. Verify that retry requests only once, entered data remains intact, focus moves to newly displayed errors when appropriate, and successful sections remain usable. Test offline mode, slow responses, expired sessions, permission changes, and a service that fails after cached content has loaded.
Exercise
A dashboard has five independent cards. One card times out, while the other four load normally. What should the interface do?
Show answer
Keep the four successful cards visible. Replace the failed card with a specific message, such as “Usage data could not be loaded,” and provide a retry action if another request could succeed. Do not replace the entire dashboard with a full-page error.