这是我当前的代码,
服务
export class ApiServiceFunction {
constructor(
private http: HttpClient
) { }
// in app.ts for test connection
testConnection() {
return this.http.get(`${environment.apiUrl}/connection`)
.pipe(
catchError(err => {
console.log('Handling error locally and rethrowing it...', err);
return throwError(err);
})
);
}
}
component.ts
private testConnection() {
this.ApiServiceFunction.testConnection()
.subscribe(
data => {
console.log('data ', data)
},
error => {
console.log('error ',error)
},
);
}
控制台中显示错误
本地处理错误并重新抛出错误...未知错误
app.component.ts:190错误未知错误
zone-evergreen.js:2845 GET http://172.16.0.115:8080/connection net :: ERR_INTERNET_DISCONNECTED
我想进行错误测试“ ERR_INTERNET_DISCONNECTED”
我们可以手动处理。
您应该添加赋予我们用户状态的方法
然后在catchError
块句柄行为
get isOnline() {
return navigator.onLine;
}
testConnection() {
return this.http.get(`${environment.apiUrl}/connection`)
.pipe(catchError(e => {
if (!this.isOnline) {
return throwError('ERR_INTERNET_DISCONNECTED')
}
return throwError(e)
})
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句