Sub topic 9
Error Messages and Recovery
Assume you know basic HTML and CSS, and you understand controls, labels, and common interface states. This chapter focuses on what happens when an action fails and how the interface helps the user continue.
An error is a mismatch between the user's intended result and the system's actual result. A good error response does more than announce failure. It explains what happened, limits uncertainty, and gives the user a practical next step.
Treat errors as part of the interaction loop
Users act because they have a goal. They then inspect the result and decide what to do next. An error interrupts that loop. The interface must help users cross the gap between the action they took and the state the system reached.
Place feedback near the affected control or content. A message at the top of a long page may be easy to miss. A message beside the invalid field helps the user connect the problem with its cause.
Describe the problem and the recovery
Useful error messages answer two questions:
- What happened?
- What can the user do now?
Add the cause when it is known and safe to reveal. For example, “Enter a valid email address” is more useful than “Invalid input.” For a server failure, say “We could not save your changes” rather than exposing a stack trace or database error.
| Weak message | Better message | Recovery |
|---|---|---|
| Invalid input | Enter a date in the format YYYY-MM-DD. | Correct the date |
| Error 403 | You do not have permission to edit this file. | Request access or cancel |
| Save failed | Your changes were not saved. Check your connection and try again. | Retry or copy the changes |
| Something went wrong | We could not load your orders. | Retry loading or contact support |
Avoid blaming the user. “You entered the wrong value” makes the person responsible for a system decision. Prefer “This value must contain at least eight characters.”
Do not claim that a retry will work when the problem is permanent. A network timeout may be temporary. A missing permission usually needs a different action. Match the recovery option to the failure.
Preserve work and support safe retries
An error should not erase information the user has already entered. Keep form values, show which fields need attention, and move focus to the first invalid field when appropriate.
Retries also need care. A failed read request is often safe to repeat. A failed payment or order request may have reached the server even if the response was lost. In that case, a blind retry could create a duplicate. Use an idempotency key, a request identifier that lets the server recognize repeated attempts as the same operation, when the operation can change data.
For long operations, keep the pending state visible. Disable the submit control while a request is in progress, but restore it after failure so the user can retry. Explain whether the system has completed, failed, or is still checking.
<form id="profile-form">
<label>
Display name
<input id="display-name" name="displayName" required minlength="2">
</label>
<p id="field-error" role="alert"></p>
<button id="save" type="submit">Save profile</button>
<p id="status" role="status"></p>
</form>
<script>
const form = document.querySelector("#profile-form");
const input = document.querySelector("#display-name");
const fieldError = document.querySelector("#field-error");
const button = document.querySelector("#save");
const status = document.querySelector("#status");
form.addEventListener("submit", async (event) => {
event.preventDefault();
fieldError.textContent = "";
status.textContent = "";
if (input.value.trim().length < 2) {
fieldError.textContent = "Enter at least two characters.";
input.focus();
return;
}
button.disabled = true;
status.textContent = "Saving…";
try {
await new Promise((resolve, reject) => {
setTimeout(() => {
Math.random() < 0.25
? reject(new Error("temporary failure"))
: resolve();
}, 700);
});
status.textContent = "Your profile was saved.";
} catch {
status.textContent =
"Your profile was not saved. Check your connection and try again.";
} finally {
button.disabled = false;
}
});
</script>The field message is associated with the field by proximity, and focus returns to the input when validation fails. The status region communicates the result without requiring the user to search the page. role="alert" is appropriate for an immediate validation problem; use it sparingly because frequent alerts can interrupt screen-reader users.
Audit error feedback
For each failure path, check the following:
- Is the failed action or affected content clear?
- Does the message state what happened in user-facing language?
- Does it avoid exposing secrets, stack traces, and internal identifiers?
- Is the user's work preserved?
- Is the next action visible and safe?
- Can keyboard and screen-reader users perceive the message?
- Does the interface distinguish a temporary failure from a permanent one?
An effective error state restores the user's mental model. After reading it, the user should know the current state, the likely cause, and the safest next move.
Exercise
A form submission times out after the server may have received the request. What should the interface say and why should it avoid an unconditional “Submit again” action?
Show answer
It should say that the result is unknown, preserve the form data, and offer a way to check the saved record or transaction status. An unconditional retry could create a duplicate operation.