Sub topic 9
Error Messages and Recovery
This chapter covers how an interface tells users that something went wrong and how it helps them fix it. It assumes you know the interaction loop and the gulf of evaluation, the basics of feedback and its timing, and basic HTML, CSS, and JavaScript. Error feedback is the case where feedback matters most. The user's goal has failed, and the message is often their only way to learn why.
Why error feedback is a special case
Earlier chapters treated feedback as the system's response to an action. An error is a response that says the action did not produce the intended result. The user has to complete three steps:
- Notice that something failed.
- Understand what failed and why.
- Decide what to do next.
Each step can break. If the user never notices the failure, the gulf of evaluation stays open and they may believe the action succeeded. If they notice it but cannot understand it, they feel blamed or stuck. If they understand it but have no path forward, the interface has dropped them at a dead end.
Good error design is mostly about closing those three gaps. A useful message answers three questions in order: what happened, why it happened (when that helps), and what the user can do now.
Prevent first, then report
The best error message is the one the user never needs. Constraints, covered earlier in this tutorial, prevent many errors before they happen. Examples include a date picker that cannot select impossible dates, a disabled submit button with a visible reason, and an input that accepts only digits.
Prevention has limits. Some errors cannot be known in advance, such as a network failure, a duplicate username, or a card the bank declines. Others come from rules too complex to encode in a control. Plan for both layers:
- Use constraints to remove errors that can be predicted.
- Use error feedback for the errors that remain.
Be careful with constraints that silently block the user. A disabled button with no explanation is a missing signifier. The user cannot tell whether they did something wrong or the page is broken. If a control is disabled because of a missing field, say so near the control.
Anatomy of a good error message
A good message has four parts.
| Part | Purpose | Example |
|---|---|---|
| Location | Shows where the problem is | Placed next to the field, with the field highlighted |
| Statement | Says what happened in plain words | "That email address is missing an @ sign." |
| Cause (optional) | Explains why, if it helps | "The server could not be reached." |
| Recovery | Says what to do next | "Check your connection and try again." |
Compare two versions of the same failure:
- Poor: "Error 422: Invalid input."
- Better: "Enter a date in the future. The date you chose, 12 March 2024, has already passed."
The poor version uses a code the user cannot interpret, does not say which input is wrong, and offers no recovery. The better version names the rule, shows the offending value, and implies the fix.
Guidelines for the wording
- Use plain language. Avoid codes, stack traces, and internal terms. If a code is useful for support, show it in small text after the human-readable message.
- Be specific. "Password must be at least 12 characters" beats "Invalid password."
- Do not blame the user. Write "We couldn't find an account with that email" instead of "You entered the wrong email."
- Be polite but brief. Skip jokes and apologies that add length without information. An apology is appropriate when the system is at fault, for example a server failure.
- Give an action, not just a diagnosis. End with the next step, or offer it as a button.
- Keep the user's work. Never clear a form because one field failed.
Placement and visibility
A message the user does not see is no message. Placement decides whether the user notices the failure and can connect it to its cause.
For form validation, put the message next to the field it concerns, and mark the field itself. Do not rely on a message at the top of the page alone, because users must then match messages to fields on their own. For forms with many fields, use both: a summary at the top that links to each failing field, and an inline message at each field.
Do not rely on colour alone to mark an error. Red borders are invisible to some users with colour vision deficiency. Add text, an icon, or both. This idea returns in the accessibility chapter.
The following diagram shows the path from a failed action to recovery.
When to validate
Timing affects how an error feels. There are three common choices.
| Moment | Strength | Weakness |
|---|---|---|
| While typing | Fast feedback | Flags errors before the user finishes, which is annoying |
| On leaving the field (blur) | Checks complete input and still gives early feedback | Misses problems that only appear across fields |
| On submit | Simple to build, checks everything | Late, and users may need to hunt for problems |
A common, balanced pattern validates a field on blur the first time. After a field has shown an error, it re-validates while typing so the error disappears as soon as the user fixes it. Always validate again on submit, and always validate on the server. Client-side checks are a usability aid, not a security control.
Types of errors
Different errors need different recovery.
- Slips are correct intentions executed wrongly, such as a typo or clicking the wrong button. Recovery should be quick: undo, an editable field, or a confirmation before a destructive action.
- Mistakes come from a wrong understanding, which often traces back to a faulty conceptual model. A message should correct the model, for example "Drafts are saved only on this device."
- System errors are failures the user did not cause, such as network loss or a server fault. The message should say the user is not at fault, say whether their data is safe, and offer a retry.
Matching the message to the type matters. Telling a user to "check your input" after a server failure sends them hunting for a mistake they did not make.
Recovery patterns
A message describes the problem. Recovery features let the user fix it. Several patterns recur:
- Undo. Often better than a confirmation dialog for reversible actions. Users act freely and reverse a slip with one click. Gmail's "Undo send" and the "Undo" toast after deleting an item are examples.
- Confirmation for destructive actions. Use it only when an action is hard to reverse. Overused confirmations teach users to click through without reading. Make the confirming button name the action ("Delete project") instead of "OK".
- Retry. For transient failures, put a "Try again" button in the message itself.
- Autosave and draft preservation. Keep what the user typed so a failure costs little.
- Suggestions. Offer likely corrections, such as "Did you mean gmail.com?"
- Escape routes. If the user cannot fix the problem, show a way to contact support or an alternative path.
Implementing an accessible error message
The next example combines several of the ideas above. The input is marked invalid, the message is linked to the field, and the message is announced to screen readers. It also keeps the user's input and moves focus to the first problem on a failed submit.
<form id="signup" novalidate>
<label for="email">Email address</label>
<input id="email" name="email" type="email" autocomplete="email"
aria-describedby="email-error" required>
<p id="email-error" class="error" role="alert" hidden></p>
<button type="submit">Create account</button>
</form>
<style>
.error { color: #a40000; margin: 0.25rem 0 0; }
.error::before { content: "⚠ "; }
input[aria-invalid="true"] { border: 2px solid #a40000; }
</style>
<script>
const form = document.getElementById("signup");
const email = document.getElementById("email");
const message = document.getElementById("email-error");
function showError(text) {
email.setAttribute("aria-invalid", "true");
message.textContent = text;
message.hidden = false;
}
function clearError() {
email.removeAttribute("aria-invalid");
message.textContent = "";
message.hidden = true;
}
function check() {
if (email.value.trim() === "") {
return "Enter your email address, for example [email protected].";
}
if (!email.validity.valid) {
return "That email address looks incomplete. Include an @ sign and a domain, such as [email protected].";
}
return "";
}
email.addEventListener("blur", () => {
const problem = check();
if (problem) showError(problem);
});
email.addEventListener("input", () => {
if (email.hasAttribute("aria-invalid") && check() === "") clearError();
});
form.addEventListener("submit", (event) => {
const problem = check();
if (problem) {
event.preventDefault();
showError(problem);
email.focus();
}
});
</script>Several details do real work here:
novalidateturns off the browser's default bubbles, so the page controls the wording and placement. The code still readsemail.validity.valid, so the browser's own rules are reused.aria-describedbylinks the message to the input. A screen reader reads the message when the input receives focus.role="alert"makes the message announce itself as soon as its text appears. Use it for errors that need immediate attention. For calmer updates,role="status"is the better choice.- The message has an icon and text as well as a colour, so it does not depend on colour perception.
- On a failed submit,
email.focus()moves the user to the problem. In a form with several fields, focus the first invalid one. - Nothing clears the input, so the user can correct the value instead of retyping it.
Common errors in error design
Watch for these failures when you review a design:
- Vague messages. "Something went wrong" tells the user nothing to act on. If the cause is unknown, still say what the user can try, and whether their work is saved.
- Messages that disappear too soon. A toast that fades after three seconds can be missed, and it is a barrier for screen reader and keyboard users. Keep error messages visible until the user fixes the problem or dismisses them.
- Errors far from their cause. A banner at the top for a field far down the page leaves the user searching.
- Wiping the form. Clearing all fields after one failure punishes the user and invites abandonment.
- Technical jargon. Codes and exception names are for logs, not for the user.
- Blaming language. "Invalid," "illegal," and "you failed to" read as accusations.
- Success shown on failure. The worst case is feedback that says "Saved" when the save failed. Only report success after the operation confirms it.
- Over-alerting. If everything is flagged as an urgent alert, users stop reading alerts. Reserve
role="alert"for messages that need immediate attention.
Rewriting an error: a worked example
Suppose an upload form shows this message when a file is rejected: "Error: invalid file."
Apply the anatomy from earlier in this chapter:
- Location: show the message beside the upload control, not in a distant banner.
- Statement: name the specific problem. The file is a
.heicimage, but the form accepts only JPEG and PNG. - Cause: the form accepts only certain formats.
- Recovery: tell the user what to do, and offer a way to do it.
The rewritten message reads: "photo.heic can't be uploaded. We accept JPEG and PNG files up to 10 MB. Convert the photo and choose it again." The file chooser stays open and the rest of the form keeps its values.
Notice that the rewrite is longer but faster to act on. Length is not the goal. The user should be able to act on the message without guessing.
Connecting to the audit
The final chapter of this tutorial audits a screen against all the principles. For error feedback, the audit questions are:
- Does every failure produce a visible response?
- Does the message state what happened and how to recover?
- Is it placed at or near the problem, and is the field marked?
- Is it perceivable without colour and available to assistive technology?
- Does the user keep their work, and can slips be undone?
Exercise
A "Pay now" request fails because the payment service is down. The page shows a red banner saying "Invalid request" and clears the card number field. Name three problems with this design and write a better message.
Show answer
The problems are (1) the message misdiagnoses a system error as a user input error ("Invalid request"), which sends the user looking for a mistake they did not make; (2) the form clears the card number, which makes the user redo work because of a failure they did not cause; (3) the message gives no recovery step and does not say whether they were charged. A better message: "We couldn't process your payment because our payment service isn't responding. You have not been charged. Your card details are still here. Try again in a few minutes." Include a "Try again" button next to the message.
Exercise
In the HTML example, why does the script re-validate on the "input" event only when the field already has aria-invalid, instead of on every keystroke from the start?
Show answer
Validating from the first keystroke would show errors while the user is still typing, such as "incomplete email" after one character, which is noise and feels hostile. Waiting until the first blur gives the user a chance to finish. Once an error has been shown, re-validating on each keystroke lets the error disappear as soon as the value is fixed, so the user gets immediate confirmation that the correction worked.