在这个特定的用例中,我们希望处理404,而不是将其传递给组件以在UI中显示文本消息。
// code in service
public getVariatioMarginPlugDates(): Observable<Date[]> {
return this._http
.get<Date[]>(AppSettings.VariationMarginPlugDatesUrl, { withCredentials: true })
.pipe(catchError(this._trace.handleError('GET ' + AppSettings.VariationMarginPlugDatesUrl, [])));
}
404
也由
catchError
404
非常感谢!
// edited code with solution
public getVariatioMarginEmail(runDate: string): Observable<any> {
const url = AppSettings.VariationMarginEmailUrl.replace(this._regExp, runDate);
return this._http
.get<any>(url, { withCredentials: true })
.catch(this._trace.handleError('GET ' + url, []));
}
public handleError<T>(operation = 'operation', result?: T, skipStatus = [404]) {
return (error: any): Observable<T> => {
if (skipStatus.includes(error.status)) {
return observableOf(error.status as T);
}
const errMsg = (error.message) ? error.message : error.status ? `${error.status} - ${error.statusText}` : 'Server error';
console.error(errMsg);
const dialog = this._dialogService.open({
title: 'Server Error',
content: `${operation} failed: ${errMsg}`,
actions: [ { text: 'Ok' } ]
});
try { this._loggingService.log(errMsg); } catch (error) {}
return observableOf(result as T); // return empty result
};
}