Sub topic 7
Writing Error Messages That Help
Assume you know basic UI components, asynchronous data fetching, HTML, and TypeScript. This chapter focuses on errors that occur after a user starts an action or requests data. The goal is not only to report failure. A useful error message helps the user understand what happened and choose the next step.
Classify the error first
Different failures need different messages and recovery actions. Start by identifying the cause:
- Input error: The user entered an invalid value.
- Authentication error: The user must sign in or their session expired.
- Permission error: The user is signed in but cannot perform this action.
- Not found: The requested item no longer exists or the link is incorrect.
- Network error: The device cannot reach the service.
- Server error: The service failed while handling a valid request.
- Conflict: The request conflicts with a newer change, such as an edit made by someone else.
Do not show the same message for every cause. “Something went wrong” hides information that could guide recovery.
| Error cause | Useful message | Next action |
|---|---|---|
| Invalid email | Enter a valid email address. | Correct the field |
| Permission denied | You can view this project, but only an owner can archive it. | Contact an owner or cancel |
| Network failure | We could not save because your connection was interrupted. | Retry when connected |
| Conflict | This project changed while you were editing it. | Review the latest version |
| Server failure | We could not load projects right now. | Retry or return later |
Use a three-part message
A helpful error message usually answers three questions:
- What happened?
- What does it affect?
- What can the user do now?
For example:
“Your changes were not saved because the connection was interrupted. Check your connection and try again.”
The first sentence identifies the result. The reason gives useful context. The final sentence gives a recovery action.
Keep the primary message short and specific. Put technical details, such as a request ID, behind a details control or in a support view. Do not expose stack traces, database names, or internal error codes in the main interface.
Use language that describes the product rather than blaming the user. “Enter a date in the format YYYY-MM-DD” is better than “You entered an invalid date.” Avoid promising a fix when you do not know the cause. “The service may be temporarily unavailable” is safer than “The service will be back soon.”
Connect the error to recovery
The recovery action should match the failure:
- For invalid input, place the message beside the relevant field and preserve the other entered values.
- For an expired session, offer Sign in again and explain whether unsaved work will remain.
- For a temporary network or server failure, offer Try again without clearing the current screen.
- For a permission problem, explain who can grant access or provide a safe alternative.
- For a missing item, offer a link back to the parent list.
- For a conflict, let the user compare versions before choosing which content to keep.
A recovery path can have more than one step. If saving fails, preserve the draft, explain that it is still on the device, and let the user retry. If only one panel fails while the rest of the page works, keep the successful content visible and mark the failed panel separately.
Model messages in code
Keep error classification separate from visual presentation. This makes it easier to provide consistent wording across screens.
type AppError =
| { kind: "validation"; field: string; message: string }
| { kind: "network" }
| { kind: "permission"; action: string }
| { kind: "server"; requestId?: string };
function describeError(error: AppError): {
message: string;
action: string;
} {
switch (error.kind) {
case "validation":
return { message: error.message, action: "Correct the field" };
case "network":
return {
message: "We couldn't save because your connection was interrupted.",
action: "Try again",
};
case "permission":
return {
message: `You do not have permission to ${error.action}.`,
action: "Request access",
};
case "server":
return {
message: "We couldn't complete that request right now.",
action: "Try again later",
};
}
}Make errors perceivable
Do not rely on color alone. Associate an error with its field, icon, or message in the interface. When a submission fails, move focus to the first invalid field or to a summary that explains the problem. Announce important asynchronous failures through an appropriate live region. Ensure the message remains visible long enough to read and is not replaced immediately by another status.
Test the message with the network disabled, an expired session, missing permissions, invalid input, and a server response. Confirm that users can recover without losing work.
Exercise
A project save fails because the server returns a temporary error. Write a useful message and choose a recovery action.
Show answer
“We couldn't save your changes right now. Your draft is still here. Try again.” Keep the editor contents and provide a Try again button.