2

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
  • 3
    I don't think you can; even if your application handles it without error, the browser "knows" the request has failed. Commented Jun 2, 2017 at 6:46

1 Answer 1

0

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 :)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.