Error handling and retry strategies
catchError and retry for HTTP calls, and centralizing error handling in one place instead of per call site.
Error handling and retry strategies
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- `catchError` intercepts an observable's error channel — without it, an HTTP failure propagates as an unhandled error straight to whatever subscribed, with nothing in between to react to it.
- `retry` resubscribes to the source observable on failure, up to a configured count — appropriate for transient failures (a flaky network blip), not for errors that will fail identically every time (a 404, a validation error).
- Centralizing error handling in an interceptor catches failures from every HTTP call in one place; catchError at an individual call site is still useful for a response that needs call-specific recovery logic.
An unhandled HTTP failure is still just an unhandled error
Everything about observables having a dedicated error channel, from the
fundamentals lesson, applies directly to HTTP calls: a failed request doesn’t
quietly return null — it propagates as an error through that channel, and if
nothing along the way is set up to catch it, it reaches whatever subscribed
exactly as an unhandled error would anywhere else in RxJS. A component that only
supplied a success callback to .subscribe(), with no error callback, can
silently fail to show the user anything at all when a request fails — not
because Angular hid the failure, but because nothing was listening for it.
catchError: intercepting the failure, deciding what happens next
getUser(id: string): Observable<User | null> {
return this.http.get<User>(`/api/users/${id}`).pipe(
catchError((err) => {
console.error('Failed to load user', err);
return of(null); // recover with a fallback value instead of propagating the error
}),
);
}
catchError sits in the pipe chain and intercepts whatever error arrives,
giving you the chance to decide what the stream should do next: return a
fallback value (as above, turning a failed request into a successful emission
of null), rethrow a different, more specific error, or re-propagate the
original. This is the direct equivalent, for the error channel, of what map
and the other operators do for successful emissions — a transformation step
you insert into the pipe.
retry: only for failures that might not repeat
this.http.get<User[]>('/api/users').pipe(
retry(2), // retry up to twice before giving up
catchError((err) => { /* handle the final failure */ return of([]); }),
);
retry(count) resubscribes to the source observable — re-issuing the same
request — up to count times if it fails, before finally letting the failure
through to whatever comes next in the pipe (typically catchError). This is
the right tool specifically for transient failures: a dropped connection
mid-request, a server momentarily overloaded, something where trying again
plausibly succeeds. It’s the wrong tool for failures that will reproduce
identically every time — retrying a 404 or a validation error just re-sends the
same doomed request repeatedly, wasting a round trip each time for a result
that was never going to change.
This lesson’s video builds retry logic specifically and makes a point of
avoiding the older, now-deprecated retryWhen operator in favor of current
guidance — worth noting if you encounter retryWhen in older material, since
retry’s configuration-object form (retry({ count, delay })) now covers the
cases retryWhen used to be needed for.
Where this logic should actually live
The previous lesson’s interceptor is often the better home for error handling
that applies broadly — catching every 401 across the whole app to redirect to
login, say, rather than repeating that check in every individual service
method’s catchError. Call-site catchError, as shown above, earns its place
specifically when the right recovery is unique to that one call — a particular
fallback value, a particular user-facing message — rather than a cross-cutting
rule that should apply everywhere HTTP calls happen.
Check yourself
A service method returns `this.http.get<User>('/api/me')` with no catchError anywhere. What happens to a component that subscribes to it, if the request fails?
The error propagates straight to the subscriber's error callback (or, with the async pipe, the error handling the pipe itself does) — nothing between the request and the subscription intercepted or transformed it. If the subscribing code didn't supply an error handler, the failure can go unhandled entirely rather than showing the user anything useful.
Why is blindly retrying a failed request a bad default for a 404 Not Found response, even though retry() would technically work?
A 404 means the resource genuinely doesn't exist at that URL — retrying the identical request will produce the identical 404 every time, so a retry just delays the inevitable failure while adding pointless extra network requests. retry() is suited to transient failures (a dropped connection, a momentary server hiccup) where the same request might plausibly succeed on a second attempt — not errors that are a stable, correct description of the actual state of things.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.