![](/img/trans.png)
[英]How do I return a translated message from a subscription in a catcherror?
[英]How can I get catchError message from service method in component?
getFinanceDetails(clientId: string, type: OnvioClientUpdateType): Observable <ClientFinance> {
return this.getClientContactDetailsById(clientId, type).pipe(mergeMap((data: any) => {
const clientFinanceDetails = this.clientContactDetailsByJsonData(data);
return of(clientFinanceDetails);
})).pipe(catchError(ExceptionHandler.handleError));
}
我正在從組件調用這個函數,這是我的服務方法。所以我的問題是,我怎樣才能得到這個在 catcherror 函數中返回的錯誤,因為 Observable ClientFinance 覆蓋了我的錯誤。
你可以使用throwError
把它扔回去。
像這樣的東西:
import { Observable, of, throwError } from "rxjs";
import { catchError, map } from "rxjs/operators";
getFinanceDetails(clientId: string, type: OnvioClientUpdateType): Observable <ClientFinance> {
return this.getClientContactDetailsById(clientId, type)
.pipe(
mergeMap((data: any) => {
const clientFinanceDetails = this.clientContactDetailsByJsonData(data);
return of(clientFinanceDetails);
}),
catchError(error => throwError("Something went wrong: ", error))
);
}
你可以處理catchError
如下
catchError(err => {
console.log('Handle error locally and rethrow it..', err);
ExceptionHandler.handleError;
return throwError('Error occurred.', err);
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.