Handling AbortError Without Noise
What you'll learn: How to catch AbortError separately so intentional fetch cancellations don't pollute your error logs or UI.
When you use AbortController to cancel a fetch request in useEffect cleanup, the browser throws a special AbortError. This is expected behavior — you told the request to abort! But if you treat this the same as network failures or server errors, you'll end up logging errors that aren't really problems, or worse, showing error messages to users for something that happened by design.
The Problem
Your error handling code catches all errors. When a user navigates away quickly or types in a search box, your cleanup function aborts the fetch. The catch block receives an AbortError, logs it, and maybe even sets an error state that displays "Something went wrong!" to the user — even though nothing actually went wrong.
The Solution
Check the error's name property in your catch block. If it's 'AbortError', simply return early without logging or setting error state:
useEffect(() => {
const controller = new AbortController();
fetchData(controller.signal)
.then(data => setData(data))
.catch(error => {
if (error.name === 'AbortError') {
// Intentional cancellation, not an error
return;
}
console.error('Fetch failed:', error);
setError(error);
});
return () => controller.abort();
}, []);
The Analogy
Think of AbortError like hanging up a phone call you no longer need. You wouldn't log "Call failed!" just because you pressed the hang-up button. Aborting is a normal part of fetch lifecycle management.
Key Takeaway: Always check for error.name === 'AbortError' in catch blocks when using AbortController, treating it as a silent, expected cancellation rather than a genuine error condition.