Sub topic 7
Writing error messages that help
An error message is the one piece of copy a user reads when something has already gone wrong. They are often stuck, or they worry they lost work. A good message tells them what happened, what it means for them, and what to do next.
This chapter assumes you can already tell the kinds of errors apart (network, permission, validation, server, and so on) and that you know the basics of TypeScript and HTML. It covers the writing and the code that delivers the words. Retry buttons, fallback content, and partial failure are covered in the next chapter.
The anatomy of a helpful message
Most useful error messages answer three questions, in this order:
- What happened? State the problem in terms of the user's task, not the system's internals.
- What does it affect? Say whether their work is saved, whether anything was lost, and what still works.
- What can they do now? Give one clear next step, ideally as a button or link.
Here is the same failure written two ways.
| Version | Text | Problem |
|---|---|---|
| Poor | `Error 500: Unexpected exception in SaveHandler` | Names internals. The user cannot act on it. |
| Poor | `Something went wrong.` | Gives no cause, no effect, and no next step. |
| Better | `We couldn't save your changes. Your edits are still on this page. Try again.` | Names the task, reassures about the data, and offers an action. |
You do not always need all three parts. A short inline message such as "Enter a date in the future" covers what happened and what to do in one sentence. Whatever you leave out, the user should never have to guess the next step.
Write in the user's terms
Describe the problem using the words the user sees in the interface. If the screen calls them "projects", the message should not say "resources" or "records".
- Say what failed, not which component failed. "We couldn't load your invoices" beats "The billing service returned an error".
- Avoid blame. Compare "You entered an invalid email" with "That email address needs an @ sign". The second one describes the fix and does not accuse.
- Skip apologies unless the system is at fault. A validation error is not your fault, and "Sorry!" there sounds odd. A server outage is your fault, and a short "We're sorry" is fine. Do not repeat it on every screen.
- Keep a calm, plain tone. Avoid exclamation marks, jokes, and alarming words such as "fatal" or "illegal". A joke in an error message tends to annoy the person who just lost their work.
- Use active voice and concrete verbs. "Check your connection" is better than "Connectivity should be verified".
Match the message to the cause
The error taxonomy from the previous chapter tells you which message pattern to use. The cause decides the content.
| Cause | What to emphasize | Typical action |
|---|---|---|
| Network or offline | Their connection, and that nothing is lost | Retry when back online |
| Timeout | The request took too long | Retry |
| Validation | The specific field and the rule | Edit the field |
| Permission | What they cannot access and why, if safe to say | Request access or sign in |
| Not found | The item is gone or the link is wrong | Go back, search, or go home |
| Server fault | It is our problem, not theirs | Retry later, status page |
| Unknown | Honesty that the cause is unclear | Retry, then contact support |
Writing from a catalog keeps these patterns consistent across the product. The code below classifies a failure and looks up the text. It runs as TypeScript in a modern runtime such as Node.js.
type ErrorKind =
| "network"
| "timeout"
| "permission"
| "notFound"
| "server"
| "unknown";
interface ApiFailure {
status?: number;
offline?: boolean;
timedOut?: boolean;
}
interface UserMessage {
title: string;
body: string;
action?: "retry" | "signIn" | "goBack" | "contactSupport";
retryable: boolean;
}
const catalog: Record<ErrorKind, UserMessage> = {
network: {
title: "You're offline",
body: "Check your connection. Your changes are kept on this page.",
action: "retry",
retryable: true,
},
timeout: {
title: "This is taking too long",
body: "The server didn't respond in time. Try again.",
action: "retry",
retryable: true,
},
permission: {
title: "You don't have access to this project",
body: "Ask the project owner to add you, or sign in with a different account.",
action: "signIn",
retryable: false,
},
notFound: {
title: "We can't find that page",
body: "It may have been moved or deleted.",
action: "goBack",
retryable: false,
},
server: {
title: "We couldn't complete that",
body: "This is a problem on our side. Try again in a few minutes.",
action: "retry",
retryable: true,
},
unknown: {
title: "Something unexpected happened",
body: "Try again. If it keeps happening, contact support.",
action: "contactSupport",
retryable: true,
},
};
function classify(failure: ApiFailure): ErrorKind {
if (failure.offline) return "network";
if (failure.timedOut) return "timeout";
if (failure.status === 401 || failure.status === 403) return "permission";
if (failure.status === 404) return "notFound";
if (failure.status !== undefined && failure.status >= 500) return "server";
return "unknown";
}
function toUserMessage(failure: ApiFailure): UserMessage {
return catalog[classify(failure)];
}
console.log(toUserMessage({ status: 503 }).title);
console.log(toUserMessage({ offline: true }).body);The retryable flag matches the AsyncState type from the state-modeling chapter. The message and the UI's behavior come from the same source, so a message never says "Try again" on an error that retrying cannot fix.
Field-level validation messages
Validation errors sit next to the thing that needs fixing, so they follow slightly different rules.
- Put the message next to the field, below it or beside it, and keep the user's input in place. Never clear a field because it was wrong.
- State the rule, not just the failure. "Password must be at least 12 characters" tells users what to do. "Invalid password" does not.
- Show the rule before the error when it is non-obvious. Format hints such as "Use MM/DD/YYYY" prevent the error in the first place.
- Validate at a sensible moment. Checking on blur (when the user leaves the field) or on submit is usually better than on every keystroke. A message that appears while someone is still typing "name@" is premature.
- Summarize on submit when there are several errors. A short list at the top that links to each field helps people find them. Move focus to the summary or to the first invalid field.
Connect each message to its field so that screen readers announce it with the field:
<label for="email">Email address</label>
<input
id="email"
type="email"
aria-invalid="true"
aria-describedby="email-error"
value="sam@example"
/>
<p id="email-error">Enter an address like [email protected].</p>For errors that are not tied to a field, such as a failed save, use role="alert" so assistive technology announces the text right away.
<div role="alert">
We couldn't save your changes. Your edits are still on this page. Try again.
</div>Use role="alert" sparingly. It interrupts whatever the screen reader is saying, so reserve it for messages that need immediate attention. The accessibility chapter covers live regions and focus in full.
Choose where the message appears
Placement is part of the message. The right location depends on the scope of the failure.
- Inline for a problem with one field or one item. It stays visible while the user fixes it.
- Banner at the top of a region for a failure that affects a section or the whole page, such as a list that cannot load. It stays until resolved or dismissed.
- Full-page error when nothing on the screen can work, such as a 404 or a crashed route.
- Toast for low-stakes, transient results. Avoid toasts for errors the user must act on, because they disappear on a timer, and a user who looks away or uses a screen magnifier may never see them.
If the message needs a decision, such as "Retry" or "Discard changes", it must persist until the user responds.
Edge cases and judgment calls
Don't expose internals. Stack traces, SQL fragments, and server hostnames do not help users, and they can help attackers. Log them for developers. If support needs to trace the failure, show a short reference ID such as "Reference: 7F3K-92" that maps to the log entry.
Be honest when you don't know. For an unclassified failure, "Something unexpected happened" is more truthful than guessing a cause. Pair it with a retry and a support route. Do not say "Please try again later" without saying how much later or what else to do.
Say when data is safe, and when it is not. Reassurance such as "Your draft is saved" is only acceptable if it is true. If the data may be lost, say so plainly and tell the user what they can still do, such as copying the text.
Avoid vague actions. Button labels like "OK" or "Dismiss" on an error give no direction. Use "Try again", "Sign in", "Go to dashboard", or "Contact support".
Don't repeat the same error many times. If five rows fail for the same reason, show one message with a count ("5 files couldn't be uploaded because they are over 10 MB") and list the files below it. Partial failure is covered in the next chapter.
Plan for translation. Do not build sentences by joining fragments in code, because word order differs between languages. Write whole messages with placeholders, such as "{count} files are too large", and allow extra space, since translated text is often 30 percent longer.
Keep it short, but not cryptic. Roughly one to two sentences fit most cases. Put extra detail behind a "Details" disclosure rather than in the main message.
A quick review checklist
Before shipping a message, read it as if you had just hit the error mid-task and check these points:
- It names the task, not the system component.
- It says what is safe or lost.
- It offers a next step with a specific label.
- It does not blame the user or use jargon.
- It matches the cause: a network error does not say "contact support", and a permission error does not say "try again".
- It appears in the right place and is announced to assistive technology.
Exercise
A user uploads a 25 MB photo to a form that accepts files up to 10 MB. The current message is "Upload failed (413)". Rewrite it as an inline message and name which of the three parts (what happened, what it affects, what to do) it covers.
Show answer
For example: "That photo is 25 MB. Choose a file under 10 MB." The first sentence says what happened and the second says what to do. A "what it affects" sentence is not needed because nothing on the form was lost. If the rest of the form is kept, you can add "Your other answers are saved."
Exercise
In the catalog code above, why does the "permission" entry set retryable: false and use the signIn action instead of retry?
Show answer
Retrying the same request with the same credentials will fail again, so a retry button would trap the user in a loop. Signing in with another account, or asking the owner for access, can change the outcome, so the message points to that action.
Exercise
A settings page shows a toast reading "Couldn't save" that disappears after 4 seconds. Give two reasons this is a poor choice and a better alternative.
Show answer
The toast can vanish before the user notices it, especially for people using screen magnifiers or screen readers, and it gives no way to act, so unsaved settings may be lost without anyone knowing. It also states no cause or next step. Show a persistent inline banner near the save control, such as "We couldn't save your settings. Your changes are still on this page. Try again", with a "Try again" button and role="alert".