Angular "Http failure response for... 0 Unknown Error" problem again
Asked Answered
C

1

13

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));
          })
        );
      }
Citify answered 14/1, 2020 at 17:51 Comment(10)
it is a CORS error. the issue is that your server is having intermittent failures and not setting the CORS header properly in these error scenarios. You need to 1. fix the server to always set the right CORS header, even in error scenarios 2. figure out what is causing the intermittent failures and try to fix it and 3. fix your front end to handle HTTP errors gracefully as they will happen... reloading the app on an http error is not graceful. You could just add in a retry or ignore the error since it retries every 60 seconds anyway.Sruti
@Sruti thanks. Do you know why the CORS not happening in the first place ? this is the part that i dont understand ?Citify
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
@Sruti Thanks. the sad thing is i am using 3rd party API. I will try to reach out to them to see if they can fix that. In the mean time, please look at my interceptor in the edit section. Thanks.Citify
@Sruti so basically, i let it retry for 5 times.then do nothing after that.Citify
frustrating that a 3rd party API isn't handling errors properly, but should work.Sruti
@Sruti Thanks.Citify
This is CORS issue. But it is not fired from your backend service. It is fired from web server due to wrong content policy for header configuration. So from your browser the status is 200. However, failed to load response. I have same case like this a few days ago, I printed out the status and I saw it is 0 (Zero) even from Chrome it is 200. So I re-configured content policy to fix this issue. Hope this will help if it was your case.Lecture
@HoangTranSon How did you re-configured content policy ? Can you give me one example ? Thank youCitify
@Citify you can checkout from here #21048752Lecture
E
1

Case 0 is caused by your outbound request not departing. This could be because CORS or some security policy stopped your request from leaving, because a service worker intercepted and failed to process the request, or because the device's internet connection failed or was suspended.

I've seen this behaviour on many of my own projects, and found that it was because the device went to standby or suspended its own connection for power saving. You can test for the event that the internet connection on the device is the problem by enabling and watching the HTTP ProgressEvents coming from the observable (failed internet connection):

These errors have status set to 0 and the error property contains a ProgressEvent object, whose type might provide further information. https://angular.io/guide/http#getting-error-details

I don't know if this necessarily answers your question, but I hope it gives you an extra avenue to investigate. I would love to hear if it helps.

Ela answered 23/6, 2022 at 1:25 Comment(2)
iam also facing this issue in my application. i have error handler function that will send these error reports to my db., there i sees lot of these errors , when i turned off the internet i can reproduce these error., but my error handler works and main api is not working on these occasion got confused if you don't mind can you answer my question , i have explained my erro here https://mcmap.net/q/910060/-http-failure-response-for-https-www-test-in-v1-messages-0-unknown-error-quot-getting-in-angular-http-request-occasionally/20300178Chokebore
Lawrence Job said, "because the device's internet connection failed". I agree. I have gotten a 0 Unknown Error when the computer that the browser is on lost its internet connection.Boiling

© 2022 - 2024 — McMap. All rights reserved.