我正在使用httpClient发送拦截请求并发送帖子请求并捕获错误。问题是我无法获取HTTP状态代码。 HttpErrorResponse的状态始终为零。
status: 0
statusText: "Unknown Error"
这是我的拦截器类:
export class HttpErrorInterceptor implements HttpInterceptor {
constructor(public toastr: ToastrService) {}
intercept(
request: HttpRequest<any>,
next: HttpHandler
): Observable<HttpEvent<any>> {
return next.handle(request).pipe(
retry(3),
catchError((error: HttpErrorResponse) => {
let errorMessage = '';
if (error.error instanceof ErrorEvent) {
// client-side error
errorMessage = `Error: ${error.error.message}`;
} else if (error.error instanceof HttpErrorResponse) {
// server-side error
errorMessage = 'Http error with code: ' + error.status;
}
console.log(error);
// Display the error
this.toastr.error(errorMessage);
return throwError(error);
})
);
}
}