2
votes

I have a problem with the error handling in my Angular 2 application. When the backend server is offline, I'm getting this uncaught error in the console:

GET http://localhost:4200/api/public/index.php/data 504 (Gateway Timeout)

My http.get in the service looks like this:

getData(): Observable<any> {

let apiUrl = `${API_URL}data`;
this.http.get(apiUrl)
  .map((res => res.json() || []))
  .subscribe(response => {
    let data = [];
    let index = 0;
    if (typeof(response.data) !== 'undefined' && response.success !== false) {
      // add index to each entry
      response.data.forEach(entry => {
        data.push({
          id: index++,
          title: entry.title,
          others: entry.others
        });
      });
      this.dataCollection = data;
      this.subject.next(data);
    }
  }, error => {
    this.subject.error("The Server could not be reached. Please wait until a connection can be established, or reload the page.");
  });

    return this.subject.asObservable();
} 

How can I prevent Angular 2 to send this error to the console?

1
I don't think you can; even if your application handles it without error, the browser "knows" the request has failed. - jonrsharpe

1 Answers

0
votes

Without knowing which dependency you are using for your HTTP requests it is almost impossible to determine your problem.

I have recently answered another question about HTTP(s) requests in TypeScript: Http Request in TypeScript

Hope this helps :)