Angular2でnet::ERR_CONNECTION_REFUSEDを処理する方法 質問する

Angular2でnet::ERR_CONNECTION_REFUSEDを処理する方法 質問する

エラー処理次のようにエラーを処理する方法を示します。

private handleError (error: Response | any) {
  // In a real world app, we might use a remote logging infrastructure
  let errMsg: string;
  if (error instanceof Response) {
    const body = error.json() || '';
    const err = body.error || JSON.stringify(body);
    errMsg = `${error.status} - ${error.statusText || ''} ${err}`;
  } else {
    errMsg = error.message ? error.message : error.toString();
  }
  console.error(errMsg);
  return Promise.reject(errMsg);
}

API サーバーにアクセスしたいのですが、サーバーが起動していません。次のエラーが発生しました:

http://localhost:3000/api/heroes net::ERR_CONNECTION_REFUSED

する必要があるAPIサーバーが起動していないことをユーザーに丁寧に伝えるこのエラーをどのように処理すればよいでしょうか?

エラー応答は次のとおりです:

_body:ProgressEvent
headers:Headers
ok:false
status:0
statusText:""
type:3
url:null

これを状態応答の?

ベストアンサー1

//First inject the router in the constructor


private handleError (error: Response | any) {
//Your other codes    

if (error.status == 0){ //or whatever condition you like to put
this.router.navigate(['/error']);
}
}

おすすめ記事