【发布时间】:2020-08-09 15:52:41
【问题描述】:
我有一个服务方法,它发出 API 发布请求以注销应用程序。
public logout(): Observable<APIResponse | ResponseError> {
return this.http
.post<APIResponse>(API_ENDPOINT + LOGOUT_URL, '{}', this.httpHeaders)
.pipe(catchError((error) => {
return of(this.handleError(error.error));
}));
}
catchError 捕获从服务器抛出的所有其他有效错误。但是,当没有网络连接或服务器关闭时,它就不起作用。
有趣的是,所有这些错误都被我的拦截器正确捕获,该拦截器对特定响应进行了一些自定义处理。
谁能告诉我为什么它在拦截器中工作但不在我的服务中?我也在拦截器中使用相同的 catchError 运算符。
这里是不接收器
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
if (req.url.endsWith('/login')) {
return next.handle(req);
}
const body = JSON.parse(<string>req.body);
const copiedReq = req.clone({ body: body });
return next.handle(copiedReq)
.pipe(tap(evt => { }), catchError((err: any) => {
if (err instanceof HttpErrorResponse) {
const httpError: HttpErrorResponse = err;
// Custom logic ...
}
return of(err);
}));
}
【问题讨论】:
-
如果你也可以显示你的拦截器会很有用
-
@PoulKruijt 我已经添加了拦截器代码
标签: angular rxjs angular-httpclient