coder
coder

Reputation: 1314

How do you handle recovery in the Angular Interceptor

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');
    })
  );

}

Upvotes: 0

Views: 226

Answers (1)

Haijin
Haijin

Reputation: 2681

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

try:

 return of(new HttpResponse({ body: {} }));

Upvotes: 1

Related Questions