I have been reading a lot of topic about this issue on stackoverflow. Most of the answer saying that this is CORS problem. I am not sure in my case so that's why i ask this question again and need serious help here.
I am developing an Angular app that hit an API every 60 seconds on an Android tablet web browser. The app working perfectly fine. But after couple hours, i started seeing errors:
0-Http failure response for https://theurl.com/api/: 0 Unknown Error
The scary part is that it happens randomly, sometime it will happen after 8,9 or 10 hours, sometimes after a day which is so hard for me to debug it. But if i reload the app, it's back to work normally. of course i dont want to do that eveytime it happens and i cant figure it out what is the root problem to fix it.
I was thinking about put a logic in interceptor. whenever i got error status : 0 then reload the app. But i am not sure its good practice..
Any suggestion will be really appreciate or at least a work around. thanks.
// Edit: i am just going to ignore after 5 times retrying when the error status is 0:
intercept(
request: HttpRequest<any>,
next: HttpHandler
): Observable<
| HttpSentEvent
| HttpHeaderResponse
| HttpProgressEvent
| HttpResponse<any>
| HttpUserEvent<any>
| any
> {
return next.handle(request).pipe(
catchError(err => {
if (err instanceof HttpErrorResponse) {
switch (err.status) {
case 401:
return this.handle401Error(request, next);
case 0:
return this.handleUnknownError(request, next);
default:
return this.router.navigate(['system-error']);
}
} else {
return throwError(err);
}
})
);
}
}
private handleUnknownError(request: HttpRequest<any>, next: HttpHandler) {
return next.handle(request).pipe(
retryWhen(errors => {
return errors.pipe(delay(2000), take(5));
})
);
}
the issue is that your server is having intermittent failures and not setting the CORS header properly in these error scenarios.
which means you're not handling errors properly on your server either. – Sruti