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