0

I am developing a simple healthcheck page on my Angular application. On the page I perform all the healthchecks like this:

  performHealthchecks() {
    for (const [service, url] of Object.entries(this.endpoints)) {
      this.logger.debug(`Checking health for ${service}`);
      this.http.get(url, { responseType: 'text', observe: 'response' }).toPromise()
        .then((response: HttpResponse<any>) => {
          this.logger.debug(`${service} HEALTHY`);
        }).catch((err: HttpErrorResponse) => {
          this.logger.error(`${service} UNHEALTHY: ${err}`);
        });
    }
  }

My problem is that instead of an HttpErrorResponse I get a string with only "Unknown Error". But on the browser console I do see the actual error, with a console log from zone.js:

http://localhost:3000/ net::ERR_CONNECTION_REFUSED zone.js:2680

I need to know the response status code to correctly identify the problem with the service, if any, but at the moment I cannot as I do not get any status code in the catch but only a useless string.

gbos
  • 503
  • 2
  • 6
  • 28
  • Seems like backend is not available. Have you tried sent request via postman, as instance? – Evgeny Gurevich Jun 06 '23 at 08:23
  • Yes I know that the backend is not available. I need to handle the error response to understand if the backend is offline, or just unhealthy or whatever it can be. In that case, the backend is offline. In other case, the healthcheck could respond with 500 and so the backend is unhealthy. How can I do this if the HttpErrorResponse is just a string and does not contain any useful info? The real question is: why HttpErrorResponse is a string that awlays says "Unknown Error" – gbos Jun 06 '23 at 10:28
  • I do get "Unknown Error" also when I have CORS errors (I can see it from the logs of zone.js) – gbos Jun 06 '23 at 10:34
  • Maybe this one will help https://www.npmjs.com/package/url-exists ? – Evgeny Gurevich Jun 06 '23 at 11:53

0 Answers0