简体   繁体   English

错误处理程序被调用两次 angular http_interceptor

[英]error handler is getting called twice angular http_interceptor

in my angular application I have a global error handler which handles all the error.在我的 angular 应用程序中,我有一个全局错误处理程序来处理所有错误。 also, I have http interceptor where I am trying to log the error when it happens.另外,我有 http 拦截器,我试图在错误发生时记录错误。 but, the http interceptor requires to return the observable when it catches error, right now I am throwing the error so internally its triggering the global error handler makes the error handler getting called twice.但是,http 拦截器需要在捕获错误时返回可观察对象,现在我正在抛出错误,因此在内部触发全局错误处理程序会使错误处理程序被调用两次。 I can remove the error handler from the http interceptor and let the global error handler does the job but I will not get the correlation ID of the request to stitch the entire requests.我可以从 http 拦截器中删除错误处理程序,让全局错误处理程序完成这项工作,但我不会获得请求的相关 ID 来拼接整个请求。 I am having two questions, when an error happens is there a way we can access request headers (so that I can get the correleation ID) or instead or throwing the error again is there anything else I can do?我有两个问题,当发生错误时,我们是否可以访问请求标头(以便我可以获取关联 ID),或者再次抛出错误,我还能做些什么吗?

here is the current interceptor.这是当前的拦截器。

catchError((error: HttpErrorResponse) => {
        this.logger.logError(
          error
          nextReq.headers.get('x-correlation-id') ?? ''
        );
        return throwError(error);

I have added a check in the global error handler, that will only call the logger when its not an instance of HTTPError.我在全局错误处理程序中添加了一个检查,它只会在它不是 HTTPError 实例时调用记录器。

if (error instanceof HttpErrorResponse) {
      // HTTP errors will be handled in interceptor itself.
    } else {
      this.logger.logException(error);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM