0

How can I recover an intercepted HTTP request in angular?

Here is an example of the catch and replace strategy with Observables

https://blog.angular-university.io/rxjs-error-handling/

The goal is to catch the error, handle it and return a "success" observable to the original subscriber. I am not able to get this to work with Angular's interceptor.

This is dumbed down code but if I return an observable it does not hit the success of the original subscriber. It will hit its complete however. And the throwError works as expected.

intercept(
    req: HttpRequest<any>,
    next: HttpHandler
): Observable<HttpEvent<any>> {

return next
  .handle(req)
  .pipe(
    catchError((error, caught) => {
       return of('this does not work');
       //throwError('this works as expected');
    })
  );

}

coder
  • 1,274
  • 1
  • 13
  • 19

1 Answers1

1

interceptor needs to return a Observable<HttpEvent<any>>

try:

 return of(new HttpResponse({ body: {} }));
Haijin
  • 2,561
  • 2
  • 17
  • 30