要在Angular 9中使用HttpInterceptor显示请求的堆栈跟踪,您可以执行以下步骤:
StacktraceInterceptor
的新的HttpInterceptor类。import { Injectable } from '@angular/core';
import { HttpInterceptor, HttpHandler, HttpRequest, HttpEvent } from '@angular/common/http';
import { Observable, throwError } from 'rxjs';
import { catchError } from 'rxjs/operators';
@Injectable()
export class StacktraceInterceptor implements HttpInterceptor {
constructor() {}
intercept(request: HttpRequest, next: HttpHandler): Observable> {
return next.handle(request).pipe(
catchError((error) => {
console.error('Request failed:', error);
console.error('Stack trace:', error.stack);
return throwError(error);
})
);
}
}
app.module.ts
)中将StacktraceInterceptor
添加到提供者列表中。import { NgModule } from '@angular/core';
import { HTTP_INTERCEPTORS } from '@angular/common/http';
import { StacktraceInterceptor } from './stacktrace.interceptor';
@NgModule({
providers: [
{
provide: HTTP_INTERCEPTORS,
useClass: StacktraceInterceptor,
multi: true
}
]
})
export class AppModule { }
StacktraceInterceptor
会拦截并显示请求的堆栈跟踪信息。这是一个简单的示例,它将在控制台上打印出请求失败的错误和堆栈跟踪信息。您可以根据需求自定义这些操作。