如何忽略HTTP响应错误并按角度5的API调用原样返回响应 我正在使用角度HTTP客户端,这是我用于调用API的代码
Api处理程序:
get<T>(url:string):Observable<T> {
return this.http.get<T>(url, this.getRequestHeaders()).pipe<T>(
catchError(error => {
return this.handleError(error, () => this.get(url));
}));
}
处理错误:
protected handleError(error, continuation: () => Observable<any>) {
if (error.status == 401) {
if (this.isRefreshingLogin) {
return this.pauseTask(continuation);
}
this.isRefreshingLogin = true;
return this.authService.refreshLogin().pipe(
mergeMap(data => {
this.isRefreshingLogin = false;
this.resumeTasks(true);
return continuation();
}),
catchError(refreshLoginError => {
this.isRefreshingLogin = false;
this.resumeTasks(false);
if (refreshLoginError.status == 401 || (refreshLoginError.url && refreshLoginError.url.toLowerCase().includes(this.loginUrl.toLowerCase()))) {
this.authService.reLogin();
return throwError('session expired');
}
else {
return throwError(refreshLoginError || 'server error');
}
}));
}
答案 0 :(得分:1)
如果要继续执行404和400的错误代码,则只需要在遇到此代码时返回假的Observable即可,就像这样:
import { of } from 'rxjs/observable/of';
...
protected handleError(error, continuation: () => Observable<any>) {
if (error.status == 404 || error.status == 400) {
return of(false);
}
...
}