0
votes

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

}

1
That link is the expected behavior. The question is why doesn't it work like that with angular's interceptor? - coder
Please provide a working example of your problem. What you have here is correct but you're just saying it doesn't work. - Reactgular
if you're returning an observable of string it will not work - bubbles

1 Answers

1
votes

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

try:

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