我正在通过后端服务发送一条post
消息
return this.http.post(/*this.API_URL +*/this.NEW_QUESTION_URL,body,httpOptions)
.map(response => {
return result; //map isn't required
}
})
.catch(this.handleError); //error handler if Observable fails
在handleError
中,我抛出了一个错误
private handleError (error: HttpErrorResponse) {
console.error('WebToBackendInterfaceService::handleError', error);//I SEE THIS PRINT
let result:ServerResponseAPI = {result:"error", "additional-info":error.message};
return throwError(result);
}
我的问题-我想throwError
返回一个Observable
。我如何以及在哪里订阅此Observable,以便我可以处理错误?
我认为Observable
返回的post
与throwError
返回的Observable
不同。我已经订阅了post
的{{1}}。
我还有一个自定义的拦截器。
return next.handle(cloned)
.pipe(tap((ev: HttpEvent<any>) => {
console.log("got an event",ev);
if (ev.type === HttpEventType.Response) {
console.log('event of type Http response');
} else if (ev.type === HttpEventType.Sent) {
console.log(" event of type httpsent");
} else if(ev.type === HttpEventType.DownloadProgress){
console.log("event of type download progress");
} else if (ev.type === HttpEventType.UploadProgress) {
console.log("event of type upload progress");
} else if (ev.type === HttpEventType.User) {
console.log("event of type user progress");
}else if (ev.type === HttpEventType.ResponseHeader) {
console.log("event of type response header")
} else {
console.log("don't know type",ev.type);
}
})
,catchError(errorResponse => {
console.log("caught error from server",errorResponse) //I SEE THIS PRINT
return observableThrowError(errorResponse);
}));
}
答案 0 :(得分:0)
let subscription = this.dataService.addData(this.data)
.subscribe((data) => {
console.log("got data ", data)
},
(error) => {
console.log("got error from the Observable: ", error)
},
() => {
console.log("observable completed")
});