14

I have an angular6 application running in production, sporadically a few users are getting an error during http requests.

Http failure response for (unknown url): 0 Unknown Error response: {"isTrusted":true}

This affects users randomly, there is no pattern in the HTTP method the user or anything else as far as I can tell, I use sentry to log errors.

I already spent a lot of time searching for a solution, so far almost everything hinted at wrong cors headers. All requests are going through an api gateway where the cors headers are set.

'Access-Control-Allow-Headers': 'Content-Type, x-internal-token, Origin, Accept, X-Requested-With, If-Modified-Since, Cache-Control, Keep-Alive'
'Access-Control-Allow-Origin': 'http://example.com'
'Access-Control-Allow-Methods': 'PUT, POST, GET, DELETE, PATCH, OPTIONS'
'Access-Control-Max-Age': 3600

So far I haven't received any errors when the app is first loading and is getting some information from the api gateway, only later while the user is using the app. Even stranger I also got this error for ./assets/i18n/de.json (normal get request), which is not cors but just some static json for dynamic translations.

I'm totally out of ideas and any help is very much appreciated.

EDIT: Please read carefully; this problem only exists for some users and not all the time, this is not a general misconfiguration!

EDIT2: To further debug this issue I set up a second api-gateway (same code) that was configured to log all requests. A slight modification was made to the angular app so it would do the same request twice; one time against the real api-gateway and one time against the logging api-gateway (for some api calls). In one instance the app was able to do the request to the real api-gateway but not the logging gateway (same code, both nginx, cors header are the same).

EDIT3: The log gateway and the real api-gateway are located on different servers (different providers) and I can see the OPTIONS request in the nginx log with status 200.

EDIT4: I've moved the cors handling from api-gateway to nginx and so far I haven't received more errors.

redshark1802
  • 894
  • 2
  • 12
  • 22
  • Check and see if any of these solutions might help. https://stackoverflow.com/questions/47180634/i-get-http-failure-response-for-unknown-url-0-unknown-error-instead-of-actu – Budhead2004 Dec 04 '18 at 14:13
  • Thanks, already saw this and no solution did help. – redshark1802 Dec 04 '18 at 14:21
  • The error message cited in the question is unrelated to CORS – sideshowbarker Dec 04 '18 at 22:21
  • 1
    Have you tried to turn off AdBlock? – Wojciech K Dec 05 '18 at 09:53
  • Is there any way you can verify if the users that experience this are being stripped of their CORS header? Depending on corporate network settings this could explain why only some users would experience it. – Maartenw Dec 06 '18 at 15:23
  • Not quite sure if/how I can get the cors headers but this wouldn't explain why it is randomly not working and why one of two requests are failing when doing them parallel. – redshark1802 Dec 06 '18 at 15:24
  • have you tried other servers rather than this one? , you can run a api tester and see if it happening in any computers or not , run api test in 2 different computer and if the fail rate is similar, probably there is a problem with your server, then if this is the case you can try other server if problem is solved or not, and if server is not the problem you can continue with clients configurations – molikh Dec 07 '18 at 07:59
  • Unfortunately your error is very strange and it's hard to help what it seems to be clear to me is that this is not an angular problem it's an api problem at some point your api is not able to respond or cors fails somehow I also received this kind of response because cors was missing, the angular app seems to be fine from what you described is more like a cors error that some times cors header apply fails or is an api availability problem – Nicu Dec 08 '18 at 14:04
  • @redshark1802 I think this is the back end error. you can debug your back end what they can receive if you have to hit API with multiple system because if problem is occurred with front end then first request is not accepted – Abhishek Dec 11 '18 at 14:10
  • This issue is definetely from back-end and check whether you are using any gateway server configuration like nginx/apache – xkeshav Dec 13 '18 at 04:14
  • @redshark1802 did you ever find a solution to this problem? i'm running into the same issue using Angular 7 / AWS API Gateway / AWS Lambda. I implemented a heartbeat on the client that sends a request to the backend every 30 seconds. Intermittently this request fails with status: 0, statusText: Unknown Error, error: {"isTrusted":true}. 95+% of the time the requests are successful. – Ryan Aug 06 '19 at 05:32

2 Answers2

1

It seems like Nginx configuration issue. The following Nginx codes will add HTML header responses Access-Control-Allow-Origin: * : public web static files of domain to let other domains access these web static files without issues:

location / {
  location ~* ^.+\.(?:css|cur|json|js|jpeg|gif|htc|ico|png|txt|otf|ttf|eot|woff|svg|webp|webm|zip|gz|tar|rar)$ {
    # If request comes from allowed subdomain
    # (yourdomain.com) then we enable CORS
    # if ($http_origin ~* (https?://yourdomain\.com(:[0-9]+)?$)) {
    #  set $cors "1";
    # }

    set $cors "1";

    # OPTIONS indicates a CORS pre-flight request
    if ($request_method = 'OPTIONS') {
      set $cors "${cors}o";
    }

    # Append CORS headers to any request from 
    # allowed CORS domain, except OPTIONS
    if ($cors = "1") {
      more_set_headers 'Access-Control-Allow-Origin: $http_origin';
      more_set_headers 'Access-Control-Allow-Credentials: true';
    }

    # OPTIONS (pre-flight) request from allowed 
    # CORS domain. return response directly
    if ($cors = "1o") {
      more_set_headers 'Access-Control-Allow-Origin: $http_origin';
      more_set_headers 'Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE';
      more_set_headers 'Access-Control-Allow-Credentials: true';
      more_set_headers 'Access-Control-Allow-Headers: Origin,Content-Type,Accept';
      add_header Content-Length 0;
      add_header Content-Type text/plain;
      return 204;
    }
  }
}
East2d
  • 1,886
  • 2
  • 17
  • 28
0

I think, this will solve the problem. In json file, please update the assets property to include an 'array' instead of 'string'.

It an issue with Angular CLI beta versions vs stable releases.

"apps": [
    {
      "root": "src",
      "outDir": "dist",
      "assets": "assets"
    }
]

 "apps": [
    {
      "root": "src",
      "outDir": "dist",
      "assets": [
        "assets"
      ]
    }
]
Suresh Kumar Ariya
  • 9,516
  • 1
  • 18
  • 27
  • & Try using Access-Control-Allow-Headers: * Access-Control-Request-Headers: * Access-Control-Request-Method: GET instead of 'Access-Control-Allow-Headers': 'Content-Type, x-internal-token, Origin, Accept, X-Requested-With, If-Modified-Since, Cache-Control, Keep-Alive' & keep all the other headers same as you written above. – dinesh mulchandani Dec 05 '18 at 09:54