Problem Statement
How do you handle HTTP errors with RxJS in components and in an interceptor?
Explanation
At the component level, pipe the HTTP call through catchError to map or rethrow and show a friendly message. In an interceptor, centralize error handling, optionally retry(), and translate status codes to app errors. Always return an observable.
Code Solution
SolutionRead Only
// component
this.api.get().pipe(catchError(err=>{ this.msg='Failed'; return of([]); })).subscribe();
// interceptor
return next.handle(req).pipe(catchError(err=>{ if(err.status===401){ this.auth.logout(); }
return throwError(() => err);
}));