What i'm trying to do is to intercept a response and if here is a certain value (CHALLENGE), make a call to an other API and retry it 3 times if needed, then, if success, reply the first call and return the result to the caller, otherwise throw an error
this is my code, but it always end with ErrorObservable.create..
intercept(
request: HttpRequest<any>,
next: HttpHandler
): Observable<HttpEvent<any>> {
return next.handle(request).switchMap((event: HttpEvent<any>) => {
if (event instanceof HttpResponse && event.body.result === "CHALLENGE") {
return this.http
.post<JsonResponse>(this.urlToChallenge + event.body.data.id, {
observe: "response",
})
.pipe(
tap((result) => {
if (result.data.status !== "WAITING") {
throw "WAITING";
}
return event;
})
)
.pipe(
retryWhen((errors) => {
return errors
.pipe(delay(this.retry_interval))
.pipe(take(this.retray_time));
})
)
.pipe(
concat(
ErrorObservable.create(
new HttpErrorResponse({
error: "error",
headers: null,
status: 103,
statusText: "Error",
url: "",
})
)
)
);
}
return of(event);
});
}
}
I'm not entirely sure if I will answer your question but here we go
My idea here is whenever we go inside the "CHALLALNGE" case to have a way to count how many times we have retried the request if the request succeeds you will receive the response from the request if it fails more than 3 times you will go in the catch statement and will have to decide how to proceed
Because I'm not able to reproduce the whole example I have created a pseudo rxjs example that should give you a better idea about the approach that I'm suggesting, that look like so
A working version of the pseudo example enter link description here
You can also check out this question How to create an RXjs RetryWhen with delay and limit on tries
Side note, it's better to have multiple functions in single pipe when possible, instead of wrapping each rxjs operator in separate pipe