【发布时间】:2019-06-21 02:18:33
【问题描述】:
我正在尝试使用拦截器处理 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);
}));
}
}
【问题讨论】:
-
你添加到模块提供者列表了吗?
-
我明白了,您已添加到提供者中
-
你能试试这个吗 .pipe(tap((response: any) => { console.log(response); return response; }))
-
检查您是否能够捕捉到响应。
-
@RakeshMakluri 是的,我可以捕捉到响应,但不能捕捉到错误发生的地方
标签: javascript angular