2017-11-16 3 views
0

コードでは、私のhttp要求をインターセプトしようとしています。私がhttp接続を確立することができるときはいつも、私のインターセプターは動作しますが、net::ERR_CONNECTION_REFUSEDを受け取ったとき、eventの変数はHttpResponseのインスタンスではないので、この種のエラーは処理できません。ここに私のコードは次のとおりです。角度4 - インターセプタハンドルnet :: ERR_CONNECTION_REFUSED

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のような迎撃エラーに検出することができますどのように?

答えて

0

ちょうどいいです!

.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); 
     } 
    } 
); 
関連する問題