如何从angular中的httpInterceptor error handler服务获取组件名称?

How to get the component name from the httpInterceptor error handler service in angular?

我需要获取 angular ts 文件中发生错误的确切组件名称、函数名称和行号,但我无法从 httpInterceptor 中获取它们。请帮助我从 httpInterceptor 获取以上信息。

console.log(this.route.routeConfig.component.name);

我已经尝试了上面的方法,它给出了当前工作组件的组件名称,但我必须从 httpInterceptor 中获取组件名称。

import { Injectable, ErrorHandler, Input } from '@angular/core';
import { catchError, retry} from 'rxjs/operators';
import { Observable, throwError } from 'rxjs';

@Injectable ({ providedIn: 'root' })
export class errorHandler implements HttpInterceptor{
    constructor(){
    }

    intercept(request : HttpRequest<any>, handler : HttpHandler) : Observable<HttpEvent<any>>{
        return handler.handle(request)
        .pipe(
            retry(1),
            catchError((error : any) => {
                console.log('---->'+request.headers.get('content-disposition'));
                let errorMessage = '';
                if(error.error instanceof ErrorEvent)
                    errorMessage = `Error : `+error.error.message;
                else
                    errorMessage = `Error Code : `+error.status +`\nMessage : `+error.message;

                return throwError(JSON.stringify(errorMessage));
            })
        )
    }

}```

console.trace();

以上代码有助于获取错误发生的完整轨迹以及相应的行号、函数名称和组件名称。因此,我们可以在调试时使用 console.trace() ,这对于找出错误发生的位置更有用。