我正在尝试使用拦截器处理 HTTP 错误,但 catchError 方法似乎在发生错误时不会触发 (422)。此方法不会拦截任何错误。app.module 中的提供程序已添加(我可以读取事件和 200 个响应)。
import { Injectable } from '@angular/core';
import {
HttpRequest,
HttpHandler,
HttpEvent,
HttpInterceptor,
HttpErrorResponse, HttpResponse,
} from '@angular/common/http';
import {Observable, throwError} from 'rxjs';
import {catchError} from 'rxjs/operators';
@Injectable()
export class RequestInterceptor implements HttpInterceptor {
constructor() {}
intercept(request: HttpRequest<any>, next: HttpHandler):
Observable<HttpEvent<any>> {
return next.handle(request)
.pipe(
catchError((error: HttpErrorResponse) => {
console.log(error);
return throwError(error);
}));
}
}