Sub topic 13
Handling Submission Failures and Duplicates
Assume you already have a multi-step form with client-side validation, a review step, and a server endpoint that accepts the final data. This chapter focuses on what happens after the user selects Submit: the network fails, the server rejects the request, or the user clicks more than once.
A reliable submission flow separates three concerns:
- Form data: the values the user entered.
- Submission status: idle, submitting, succeeded, or failed.
- Request identity: a unique key that lets the server recognize retries of the same submission.
Keep the form data after failure
A failed request should change the submission status, not erase the form. The user should see what went wrong and be able to retry without entering everything again.
Common failure categories include:
| Failure | User-facing response | Retry? |
|---|---|---|
| Network timeout or lost connection | “We could not reach the server.” | Yes |
| Server error, such as HTTP 500 | “The service is temporarily unavailable.” | Yes |
| Validation rejection, such as HTTP 400 | Show the affected fields | After correction |
| Authentication or permission error | Ask the user to sign in again | Usually not immediately |
Do not display raw server errors, stack traces, or technical status codes as the main message. Log those details for diagnosis, but give the user a clear recovery action.
Prevent repeated submissions
Disable the submit button while a request is in progress. This prevents accidental double clicks, but it is only a client-side safeguard. A user can refresh the page, open another tab, or send the same request through a retry mechanism.
Use an idempotency key, which is a unique identifier attached to one intended operation. The server stores the result for that key. If it receives the same key again, it returns the original result instead of creating another record.
type SubmissionResult =
| { status: "success"; receiptId: string }
| { status: "error"; message: string };
async function submitOrder(
values: Record<string, string>,
idempotencyKey: string,
): Promise<SubmissionResult> {
const response = await fetch("/api/orders", {
method: "POST",
headers: {
"Content-Type": "application/json",
"Idempotency-Key": idempotencyKey,
},
body: JSON.stringify(values),
});
if (response.ok) {
const data: { receiptId: string } = await response.json();
return { status: "success", receiptId: data.receiptId };
}
if (response.status >= 400 && response.status < 500) {
const data: { message?: string } = await response.json().catch(() => ({}));
return {
status: "error",
message: data.message ?? "Check your details and try again.",
};
}
return {
status: "error",
message: "The service is temporarily unavailable. Try again.",
};
}Generate the key when the user begins the final submission, then reuse it for retries of that submission. Do not generate a new key for every retry. On the server, scope keys to the authenticated user or account, set an expiration period, and store enough information to return the original response.
Model the retry path
The interface should make the current state visible. During submission, show progress and prevent conflicting edits if the server is processing the exact review data. After failure, restore the form and offer a retry. After success, replace the submit action with a confirmation so the user does not wonder whether another click is needed.
Do not automatically retry every failed request. Automatic retries are safer for clearly transient failures, such as a timeout, only when the same idempotency key is reused. Never retry a request after an unknown server response without duplicate protection.
Exercise
Exercise
Why should a retry reuse the original idempotency key instead of creating a new one?
Show answer
The server can recognize the retry as the same intended operation and return the original result without creating a duplicate.