我的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将此错误发送到控制台?
我不认为你可以;即使您的应用程序无误地处理它,浏览器也会“知道”请求失败。 – jonrsharpe