Error Handling
Error Handling covers classifying failures, normalizing API errors, field validation, safe retries, Error Boundaries, and privacy-aware logging.
Course: React — Routing & API Integration Lesson: 11
Learning objectives
Classify frontend failures, normalize API errors, present safe user messages, implement retry behavior, and distinguish data errors from rendering errors.
Error categories
A SaaS frontend can encounter:
- Network failures: no response was received.
- HTTP failures: server returned 4xx or 5xx.
- Validation failures: submitted fields violate business rules.
- Authentication failures: session or credentials are invalid.
- Authorization failures: user lacks permission.
- Parsing failures: response does not match the expected format.
- Rendering failures: a component throws while rendering.
These categories should not all produce the same generic alert.
Normalize service errors
A service layer can convert transport-specific errors into a consistent shape:
export function toAppError(error) {const status = error.response?.status;if (!error.response) {return { type: "network", message: "Connection problem. Check your network." };}if (status === 401) {return { type: "unauthenticated", message: "Please sign in again." };}if (status === 403) {return { type: "forbidden", message: "You do not have access to this action." };}if (status === 404) {return { type: "not-found", message: "The requested item was not found." };}if (status === 422) {return { type: "validation", message: "Please review the submitted information." };}return { type: "server", message: "Something went wrong. Try again later." };}
Adapt the mapping to the backend's actual contract. Do not assume every API uses 422 for validation or that every 401 means the same session workflow.
Field-level validation
For forms, map server validation details to the relevant fields when the API provides a documented structure. Keep a form-level summary for errors that cannot be associated with one field. Preserve user input when safe so users do not need to re-enter everything.
Retry safely
Retries can help with transient network or server failures. They are not automatically safe for every request. Retrying a GET is generally different from retrying a payment or order-creation POST. For mutations, use backend-supported idempotency keys or explicit duplicate protection where appropriate.
Error boundaries
React Error Boundaries catch rendering errors in descendant component trees. They do not replace normal handling for rejected Promises or API responses. Handle expected request failures in the data layer and render an appropriate state. Use an Error Boundary for unexpected rendering failures.
Logging and privacy
Capture useful diagnostic context such as operation name, request correlation ID, and status code. Do not log passwords, access tokens, payment details, or unnecessary personal data. Show users a clear message; keep sensitive technical details in protected observability systems.
Common mistakes
- Swallowing errors and rendering an empty page.
- Displaying raw server stack traces to users.
- Retrying non-idempotent operations blindly.
- Treating 403 as an expired login.
- Expecting Error Boundaries to catch every asynchronous failure.
- Logging secrets while debugging.
Best practices
Normalize errors once, keep messages actionable, preserve form state, distinguish expected from unexpected failures, and make retry rules explicit.
Interview questions & answers
Interview preparation
5 questions grouped by difficulty — expand any card for a model answer and follow-up probe.
Beginner
1What is the difference between 401 and 403?
BeginnerModel answer
401signals missing or invalid authentication.403signals an authenticated caller lacking permission.2How would you normalize errors from multiple backend services?
BeginnerModel answer
Map transport-specific failures into a shared application error shape (type + safe message) in the service layer before UI rendering.
Intermediate
3What does an Error Boundary catch?
IntermediateModel answer
Rendering errors in descendant React trees—not rejected Promises or ordinary API failures, which belong in the data layer.
4What information should not be logged?
IntermediateModel answer
Passwords, access tokens, payment details, and unnecessary personal data—keep diagnostics free of secrets.
Advanced
5Why can retrying a POST be dangerous?
AdvancedModel answer
A second POST may create duplicate resources or charges unless the operation is idempotent or protected against duplicates.
Practice
Implement an order form that displays field validation, a permission error, a network error with retry, and a generic server failure. Add tests for each error category.