2017-06-02 73 views
1

我的Angular 2应用程序中的错误处理存在问题。 当后端服务器处于脱机状态,我在控制台收到此未被捕获的错误:角度2:未捕获504错误(网关超时)

GET http://localhost:4200/api/public/index.php/data 504(网关超时)

我http.get在服务看起来是这样的:

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(); 
} 

如何防止Angular 2将此错误发送到控制台?

+3

我不认为你可以;即使您的应用程序无误地处理它,浏览器也会“知道”请求失败。 – jonrsharpe

回答

-1

您是否已尝试使用其他浏览器。 Chrome显示错误,但Firefox将其显示为警告。

你在Firefox中得到什么?

+1

请不要将评论发布为“答案”。 – jonrsharpe

+0

正确。我正在使用谷歌浏览器。在Firefox中,我只收到警告:“XML语法错误” –

+0

我会接受你的答案,因为无法在Chrome和Firefox中隐藏错误,所以不会显示。多谢,伙计。 –