I have been researching extensively on this issue, particularly on Stack Overflow. Many of the responses point to it being a CORS problem, but I am uncertain if that is the case in my situation. Therefore, I am reaching out for help once again and would greatly appreciate any serious assistance.
I am currently developing an Angular application that accesses an API every 60 seconds on a web browser of an Android tablet. Initially, the app functions perfectly fine. However, after a few hours, I start encountering errors:
0-Http failure response for https://theurl.com/api/: 0 Unknown Error
The concerning aspect is that these errors occur randomly; sometimes they appear after 8,9, or 10 hours, other times after a full day, making it difficult for me to troubleshoot effectively. Nevertheless, when I reload the app, it starts working normally again. Unfortunately, I do not want to rely on manual reloading each time an error pops up, and I am struggling to identify the root cause of the issue.
I was considering implementing a logic in the interceptor to automatically reload the app whenever it encounters an error status of 0. However, I am unsure if this approach is considered best practice...
Any suggestions or workarounds would be highly appreciated. Thank you!
// Edit: I have decided to implement a mechanism where the app will stop retrying after encountering error status 0 five times:
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));
})
);
}