Angular Can';t使用拦截器捕获HTTP错误代码

Angular Can';t使用拦截器捕获HTTP错误代码,angular,http,interceptor,Angular,Http,Interceptor,我试图使用拦截器捕获所有401错误并将用户从会话中踢出,但我只能通过检查每个查询来捕获它 这项工作: 服务: const httpOptions: { headers; observe; } = { headers: new HttpHeaders({ 'Content-Type': 'application/json' }), observe: 'response' }; createSomething(something: Something): Observable<

我试图使用拦截器捕获所有401错误并将用户从会话中踢出,但我只能通过检查每个查询来捕获它

这项工作:

服务:

const httpOptions: { headers; observe; } = {
    headers: new HttpHeaders({ 'Content-Type': 'application/json' }),
    observe: 'response'
};

createSomething(something: Something): Observable<HttpEvent<Something>> {

    return this.http.post<Something>(this.urlSomething, something, httpOptions)
}
@Injectable()
export class JwtInterceptor implements HttpInterceptor {
    constructor(private authenticationService: AuthenticationService) { }

    intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {

        return next.handle(request).pipe(catchError(err => {

            if (err.status === 401) {

                setTimeout(() => {
                    this.authenticationService.logout();
                    location.reload(true);
                }, 1000);
            }

            const error = err.error.message || err.statusText;
            return throwError(error);
        }))
    }
}

但是,如果我尝试使用拦截器,但它没有捕获错误,那么会出现什么问题

拦截器:

const httpOptions: { headers; observe; } = {
    headers: new HttpHeaders({ 'Content-Type': 'application/json' }),
    observe: 'response'
};

createSomething(something: Something): Observable<HttpEvent<Something>> {

    return this.http.post<Something>(this.urlSomething, something, httpOptions)
}
@Injectable()
export class JwtInterceptor implements HttpInterceptor {
    constructor(private authenticationService: AuthenticationService) { }

    intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {

        return next.handle(request).pipe(catchError(err => {

            if (err.status === 401) {

                setTimeout(() => {
                    this.authenticationService.logout();
                    location.reload(true);
                }, 1000);
            }

            const error = err.error.message || err.statusText;
            return throwError(error);
        }))
    }
}
@Injectable()
导出类JwtInterceptor实现HttpInterceptor{
构造函数(私有authenticationService:authenticationService){}
拦截(请求:HttpRequest,下一步:HttpHandler):可观察{
返回next.handle(request).pipe(catchError(err=>{
如果(错误状态===401){
设置超时(()=>{
this.authenticationService.logout();
位置。重新加载(true);
}, 1000);
}
const error=err.error.message | | err.statusText;
返回投掷器(错误);
}))
}
}


非2xx状态代码实际上不是错误。哦,所以没有解决办法?我想我应该在后端做一些修改你说的“没有解决办法”是什么意思?这只是意味着你不能用
catchError
捕捉它,你仍然可以用其他流操作符处理它。@jornsharpe它被HttpClient视为一个错误。我希望第一个不会工作,因为它使用成功回调来测试401响应。我希望第二个能很好地工作。我自己在所有项目中都使用这样的拦截器。将tap()添加到拦截器时会发生什么?它是否正确注册为拦截器?我觉得很奇怪,你把它命名为JwtInterceptor,尽管它与JWT无关。这和你的问题有关吗?@JBNizet我想我在考虑fetch或axios。在这种情况下,我同意你的看法,第一个片段不应该工作,第二个应该可以。