Learn professional error handling in React. Discover how to catch API failures, store error messages in state, and provide clear UI feedback to your users.
Previously in this course, we covered handling loading states to keep users informed while waiting for data. Now, we’ll address the inevitable reality of web development: things go wrong. Whether it's a 404, a server outage, or a lost connection, your application needs to handle these failures gracefully rather than leaving the user staring at a blank screen.
When you use the fetch API, it only rejects the promise if there is a network error (like the user being offline). If the server responds with a 404 Not Found or 500 Internal Server Error, fetch considers the request "successful."
To provide robust error handling, you must manually check the response.ok property. If it’s false, you throw an error yourself. By wrapping these operations in a try-catch block, you can intercept both network failures and your own custom errors, then store that information in your component's state to provide UI feedback.
Let’s update our MovieBrowser component. We need a new piece of state to hold the error message.
JSXimport { useState, useEffect } from CE9178">'react'; function MovieBrowser() { const [movies, setMovies] = useState([]); const [isLoading, setIsLoading] = useState(false); const [error, setError] = useState(null); // New state for error useEffect(() => { async function fetchMovies() { setIsLoading(true); setError(null); // Reset error on new request try { const response = await fetch(CE9178">'https://api.example.com/movies'); // Manually check for HTTP errors if (!response.ok) { throw new Error(CE9178">'Failed to fetch movies. Please try again later.'); } const data = await response.json(); setMovies(data); } catch (err) { // Catch network errors or our thrown error setError(err.message); } finally { setIsLoading(false); } } fetchMovies(); }, []); if (error) return <div className="error-box">{error}</div>; if (isLoading) return <div>Loading...</div>; return ( <ul> {movies.map(movie => <li key={movie.id}>{movie.title}</li>)} </ul> ); }
setError(null) at the start of your effect. If a user retries a failed request, you don't want the old error message lingering on the screen.finally Block: Use this to ensure setIsLoading(false) runs regardless of whether the request succeeded or failed. This prevents your app from being stuck in a permanent loading state.if (error) before rendering the data, you prioritize informing the user that something went wrong.While we are focusing on the frontend here, remember that designing error responses clients can actually use on the backend is equally important for a stable ecosystem. If you're interested in more advanced patterns, the TypeScript Result Pattern is a fantastic way to handle errors without relying on exceptions at all.
Modify your current movie browser project:
error state variable.fetch logic to include a try-catch block.response.ok is false, throw a descriptive error message.error exists, render a paragraph with the error text in red.fetch doesn't throw on 404/500 errors. Always check response.ok.finally block: If you only set setIsLoading(false) inside the try block, your spinner will spin forever if the request crashes.We've successfully moved from "happy path" coding to production-ready API interaction. By using try-catch and response.ok, we ensure that our app can recover from network issues. Storing these errors in state allows us to use conditional rendering to show the user exactly what they need to know, maintaining a professional and reliable user experience.
Up next: We will learn how to use cleanup functions in useEffect to prevent memory leaks and handle component unmounting.
Stop overwhelming your server with every keystroke. Learn how to implement debouncing in React to optimize API performance and create snappier UIs.
Read moreMaster the fetch API in React to retrieve external data. Learn to perform asynchronous requests and store JSON responses in your component state effectively.
Managing Errors
Prop Drilling and Context API
Polishing the UI
Finalizing the Movie Browser
Review of Component Lifecycle
Review of State Management
Building a Modal Component
Introduction to PropTypes
Performance Optimization Basics
Handling Browser History
Working with LocalStorage
Building a Favorites List
Handling Media in React
Introduction to Testing
Debugging React Apps
Deployment Basics
Using External Libraries
Advanced