请忽略变量名称和格式,因为我刚刚更改了它们.
我一直在try 为一个可观察对象实现RxJS错误处理,该对象执行一个操作(用户单击),然后从我们的表单发送请求对象,并将其传递给一个HTTP调用,如下所示.
问题:如果我在我们的网页上发送了一个后端不存在的请求(来自表单),它会失败,并得到一个错误(太棒了!),但如果我try 在try 失败后重新提交正确的请求,应用程序就会挂起并加载,并且不会做任何事情.
serivce.ts
行文件
sendRequest$ = this.exampleRatingSubjectAction$.pipe(switchMap(exampleRequest => this.http.post<exampleresponse>(this.PCFUrl + '/example',exampleRequest).pipe(
catchError(this.handleError)
)),
tap(data => console.log(data)),
);
sendExampleRequestEconomy$ = this.exampleSubjectActionEconomy$.pipe(switchMap(exampleRequest=> this.http.post<exampleresponse>(this.PCFUrl + '/example',exampleRequest)
.pipe(
catchError(this.handleError)
)),
tap(data => console.log(data)),
);
private handleError(err: HttpErrorResponse): Observable<never> {
// in a real world app, we may send the server to some remote logging infrastructure
//instead of just logging it to the console
let errorMessage: string;
if (err.error instanceof ErrorEvent) {
// A client-side or network error occured. Handle it accordingly
errorMessage = `An error occured: ${err.error.message}`;
} else {
// The backend returned an unsuccessful response code.
// the resposne body may contain clues as to what went wrong,
errorMessage = `Backend returned code ${err.status}: ${err.message}`;
}
console.error(err);
return throwError(errorMessage);
}
component code:
个
exampleResponsePriority$ = this.dataService.sendExampleRequest$
.pipe(
map(data => this.exampleResponsePriority = data),
tap(data => {
this.showPageLoader = false;
this.calculatorService.checkMinOrDeficitRate(data);
}),
catchError( err => {
this.showPageLoader = false;
// await this.presentAlert(err);
this.errorMessageSubject.next(err);
console.log("priority")
return EMPTY;
}));
exampleResponseEconomy$ = this.dataService.sendExampleRequestEconomy$
.pipe(
map(data => this.exampleResponseEconomy = data),
catchError( err => {
this.showPageLoader = false;
// await this.presentAlert(err);
this.errorMessageSubject.next(err);
console.log("economy")
return EMPTY;
}));
我唯一的猜测是,我们的操作流已经在第一次单击时使用请求对象进行了更新,那么当来自后端的响应是500时,它可能会被困在上面,不允许重新提交?
我曾try 在失败的请求上更新操作流,但无法使其工作.
我觉得我只需要几行代码就可以了!
任何帮助都是最好的.