Problem Statement
How do you handle errors in HTTP streams and add retry with backoff?
Explanation
Use catchError to map an error to a safe value or rethrow. For transient errors (like 5xx), use retryWhen + delay/backoff. Always surface user-friendly messages and log technical details.
Code Solution
SolutionRead Only
this.api.getData().pipe(
retryWhen(errors => errors.pipe(
scan((acc) => acc + 1, 0),
takeWhile(retryCount => retryCount < 3),
delayWhen(retryCount => timer(2 ** retryCount * 500))
)),
catchError(err => {
this.toast('Could not load data');
return of([]);
})
).subscribe(data => this.items = data);