【发布时间】:2018-09-19 21:02:33
【问题描述】:
我想要一个 HTTPInterceptor 来处理所有错误。我的代码如下所示:
return next.handle(req)
.pipe(
catchError((response: any) => {
if (response instanceof HttpErrorResponse) {
if (response.status === 401) {
console.log('error catched';
return empty();
}
}
return throwError(response);
})
);
但在运行代码时会产生 401 错误
return this.http
.post(`${environment.baseAPIUrl}/identity/login`, { 'user': user, 'pwd': pwd})
.pipe(
tap(res => this.setToken(res)),
shareReplay()
);
输出是这样的:
POST http://xyz 401 (Unauthorized)
error catched
所以我可以处理错误,但我无法阻止错误输出。我做错了什么?
最好的
英格玛
【问题讨论】:
标签: angular angular-http-interceptors catch-exception