【发布时间】:2020-09-23 13:08:06
【问题描述】:
我正在尝试使用 observable 处理 HttpError 或 HttpErrorResponse 但我所能做的就是处理所需的 HttpResponse 但我还需要避免这种情况
我需要能够在响应出现问题时提醒用户,也许他们可以重试...
this.addService.log()
.subscribe(event => {
if (event instanceof HttpResponse) {
//do something
} else if ( event instanceof HttpErrorResponse){
console.log(`couldn't httperrorresponse`)
} else if (event instanceof ErrorEvent){
console.log(`couldn't errorevent`)
}
});
}
我已经尝试将整个东西封装在一个 try catch 中,但它仍然没有抛出错误
【问题讨论】:
-
您需要在
subscribe的错误回调中编写错误处理程序,因为任何服务器端错误都将由错误回调处理,因此您不会在成功回调中获得任何HttpErrorResponse实例你上面写的
标签: angular observable httprequest httpresponse http-error