Angular 5 HttpInterceptor错误处理,首先调用调用方的错误处理

Angular 5 HttpInterceptor错误处理,首先调用调用方的错误处理,angular,angular-http-interceptors,angular-httpclient,Angular,Angular Http Interceptors,Angular Httpclient,我有一个全局HttpInterceptor,带有一个catch块,用于处理HttpErrorResponse。但我的要求是,当一个服务进行http调用并且还有一个错误处理程序时,我希望该服务上的错误处理程序首先启动。如果此服务上没有错误处理程序,那么我希望全局HttpInterceptor错误处理程序来处理它 示例代码: Http拦截器: @Injectable() export class ErrorHttpInterceptor implements HttpInterceptor { c

我有一个全局HttpInterceptor,带有一个catch块,用于处理HttpErrorResponse。但我的要求是,当一个服务进行http调用并且还有一个错误处理程序时,我希望该服务上的错误处理程序首先启动。如果此服务上没有错误处理程序,那么我希望全局HttpInterceptor错误处理程序来处理它

示例代码:

Http拦截器:

@Injectable()
export class ErrorHttpInterceptor implements HttpInterceptor {

constructor(private notificationService: NotificationService) {}

intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(req)
  .catch(
    error => {
      if (error instanceof HttpErrorResponse) {
        this.notificationService.error('Error', 'Error in handling Http request');
      }

      return Observable.empty<HttpEvent<any>>();
    }
  );
 }
}
在这种情况下,ErrorHttpInterceptor发出通知,然后userService错误处理也发出错误通知


但是在我的用例中,我希望ErrorHttpIntercetor只在底层订阅没有处理错误时处理错误。有办法做到这一点吗?

解决方法之一是通过请求传递
httpHeaders

 request() {  
    const url = 'GoalTree/GetById/'; 
    let headers = new HttpHeaders();
    headers = headers.append('handleError', 'onService');
    this.http.get(url, {headers: headers})
      .pipe(
      map((data: any) => {
        this.showError = false; 
      }),
      catchError(this.handleError)
      )
      .subscribe(data => {
        console.log('data', data);
      })
  }
Interceptor.ts:

intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {

    this.spinnerService.show();

    return next.handle(req).do(
      (event: HttpEvent<any>) => {
        if (event instanceof HttpResponse) {
          this.spinnerService.hide();
        }
      },
      (err: any) => {
        if (req.headers.get('handleError') === 'onService') {
          console.log('Interceptor does nothing...');
        } else {
          console.log('onInterceptor handle ', err);
        }

      }
    );
  }

intercept(req:HttpRequest

这是迄今为止我提出的最好的解决方案

在拦截器中,在错误对象上创建新的订阅属性,然后重新显示错误:

return next.handle(authReq).pipe(
    catchError((err: any, caught: Observable<HttpEvent<any>>) => {
        err.defaultHandler = timer(0).subscribe(() => {
            this.notify.error('Server Error', 'Server Error Message');
        });
        return throwError(err);
    })
);

谢谢,这是一个聪明的方法。但这需要重构大量代码来检查我们在服务层处理错误的位置,并将头附加到调用中。@Nani,这可以稍微简化。检查头的存在性(无需检查头值),如:
if(req.headers.has('inService')){}
Woo,谢谢@Yerkon
return next.handle(authReq).pipe(
    catchError((err: any, caught: Observable<HttpEvent<any>>) => {
        err.defaultHandler = timer(0).subscribe(() => {
            this.notify.error('Server Error', 'Server Error Message');
        });
        return throwError(err);
    })
);
this.authService.login(authRequest).subscribe((x) => {
  this.navigateDefaultRoute();
}, (err) => {
  // Prevent default error from being handled
  err.defaultHandler.unsubscribe();

  this.notify.error('Invalid Credentials', 'Email and password did not match');
});