如何在Angular 7中处理`err_connection_refused` HTTP错误?

时间:2019-04-09 11:43:39

标签: angular

如果HTTP请求有一个err_connection_refused错误,我想向用户显示一条消息。因为服务器已断开连接,所以出现此错误。

http.get().subscribe(
    (response) => {

    },
    error => {
        // some  code to know error is err_connection_refused
    }
);

1 个答案:

答案 0 :(得分:0)

这是您想要的方法。只需在控制台中查看err对象,看看连接被拒绝的状态是什么:

http.get().pipe(
  // here we can stop the error being thrown for certain error responses
  catchError(err: HttpErrorResponse => {
    console.log(err)
    if (err.status == 404) return of(null)
    else throwError(err)
  })
).subscribe(
    (response) => {
    },
    // if the error is thrown (or not caught if you do not use catchError) we hit this block
    error => {
        console.log(err.status)
        // some  code to know error is err_connection_refused
    }
);