Angular 4 - 拦截器句柄 net::ERR_CONNECTION_REFUSED

Angular 4 - interceptor handle net::ERR_CONNECTION_REFUSED

在下面的代码中,我尝试拦截我的 http 请求。每当我可以建立http连接时,我的拦截器就可以工作,但是当我得到net::ERR_CONNECTION_REFUSED时,event变量不是HttpResponse的实例,所以我无法处理这种错误。这是我的代码:

intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    this.changeStatus(false);
    this.requests_count++;
    const my_req = req.clone({
        url: API_PATH + req.url
    });
    return next
        .handle(my_req)
        .do(event => {
            if (event instanceof HttpResponse) {
                this.requests_count--;
                if (!this.requests_count)
                    this.changeStatus(true);
            }
        });
}

如何在拦截器中检测像 net::ERR_CONNECTION_REFUSED 这样的错误?

刚刚收到!

.do(
    event => {
        if (event instanceof HttpResponse) {
            this.requests_count--;
            if (!this.requests_count)
                this.changeStatus(true);
        }
    },
    error => {
        if (error instanceof HttpErrorResponse) {
            this.requests_count--;
            if (!this.requests_count)
                this.changeStatus(true);
        }
    }
);