How to catch an error on a Request, then open a modal, then retry when modal closes with RxJS
I think that you need to return something an observable even in the case of the 401 error:
public errorHandler(res: Response, ob: Observable<any>): Observable<Response> {
if (res.status === 401) {
let closedSubject = new Subject();
this.modalService.open(new ModalConfig({
content: LoginModalComponent,
close: () => {
closedSubject.next();
}));
return ob.retryWhen(() => closedSubject);
}
else {
return Observable.throw(res.json());
}
}
See this article for more details: https://jaxenter.com/reactive-programming-http-and-angular-2-124560.html.
Edit
The problem is that the second parameter of the catch
callback isn't the source observable. This source observable corresponds to the value of its source
property:
return ob.source.retryWhen((errors) => closedSubject);
See the working plunkr: https://plnkr.co/edit/eb2UdF9PSMhf4Dau2hqe?p=preview.
I guess retryWhen
operator should help.