使用 Angular 2 和 rjxs 检查端口上的超时错误

发布于 2025-01-15 03:10:26 字数 614 浏览 4 评论 0原文

我想在向端口发送请求时检查请求是否超时。我只想在请求超时时打印错误。

myService.service.ts

checkStatus() : Observable<any> {
    return this.http.get<any>('http://127.0.0.1:3001'); // simple example
}

myComponent.ts

this.myService.checkStatus.subscribe(() => {}, 
  (err) => {
     // if the timeout error is print error
  }
}

我只想在修复超时错误时从屏幕上删除错误。如果返回错误 404 或其他错误,我不希望执行该操作并将其打印到屏幕上。

简而言之,如下所示,

  1. 将向端口发送请求并检查是否有超时错误。
  2. 如果存在超时错误,则会在屏幕上打印错误。(404或不同的错误不会打印,仅打印超时错误)
  3. 如果存在超时错误,将检查错误是否仍然存在,如果错误继续存在如果没有发生,屏幕上的文本将被删除。

I want to check if the request is timeout when I send a request to the port. I just want to print an error when the request is timeout.

myService.service.ts

checkStatus() : Observable<any> {
    return this.http.get<any>('http://127.0.0.1:3001'); // simple example
}

myComponent.ts

this.myService.checkStatus.subscribe(() => {}, 
  (err) => {
     // if the timeout error is print error
  }
}

I just want the error to be removed from the screen when the timeout error is fixed. if error 404 or a different error returns, I do not want the operation to be performed and printed to screen.

in short, it is as follows

  1. A request will be sent to the port and checked for a timeout error.
  2. If there is a timeout error, an error will be printed on the screen.(404 or different errors will not print, only timeout error)
  3. if there is a timeout error, it will be checked if the error persists, and if the error does not occur, the text on the screen will be removed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

回梦 2025-01-22 03:10:26

使用 retryWhen 以一定的时间间隔检查超时错误,这里是一个内部可观察值,外部可观察值仅用于为 ui

myService.service.ts

  checkStatus() : Observable<any> {
    return Observable.create((observer) => {
      this.http
        .get<any>('http://127.0.0.1:3001')
        .pipe(
          map(() => {
            // Success case...
            observer.next(null);
          }),
          retryWhen((err) => {
            return err.pipe(
              switchMap((err, i) => {
                
                // If it is a time out error,
                // then emit message to display
                // and set next retry attempt
                // by returning timer observable
               
                if(err.status === 504){
                   observer.next("timeout error!");
                   return timer(2000);
                }
                // If it's not a timeout error
                // then return null or anything you want
                // On basis of what you can remove message 
                // from ui

                observer.next(null);
                
                // Terminating retry...
                throwError(() => 'It's not a timeout error!');
              })
            );
          }),
          catchError((err) => {
            return throwError(err);
          })
        )
        .subscribe(console.log);
    });
  }

myComponent 发出消息.ts

this.myService.checkStatus.subscribe(
  (res) => {
     // if the timeout error is print error
  }, 
  (err) => {
    
  }
}

Using retryWhen to check timeOut error with certain interval,and which is here a inner observable,and outer observable only to emit mesages for ui

myService.service.ts

  checkStatus() : Observable<any> {
    return Observable.create((observer) => {
      this.http
        .get<any>('http://127.0.0.1:3001')
        .pipe(
          map(() => {
            // Success case...
            observer.next(null);
          }),
          retryWhen((err) => {
            return err.pipe(
              switchMap((err, i) => {
                
                // If it is a time out error,
                // then emit message to display
                // and set next retry attempt
                // by returning timer observable
               
                if(err.status === 504){
                   observer.next("timeout error!");
                   return timer(2000);
                }
                // If it's not a timeout error
                // then return null or anything you want
                // On basis of what you can remove message 
                // from ui

                observer.next(null);
                
                // Terminating retry...
                throwError(() => 'It's not a timeout error!');
              })
            );
          }),
          catchError((err) => {
            return throwError(err);
          })
        )
        .subscribe(console.log);
    });
  }

myComponent.ts

this.myService.checkStatus.subscribe(
  (res) => {
     // if the timeout error is print error
  }, 
  (err) => {
    
  }
}
薄凉少年不暖心 2025-01-22 03:10:26

你可以做这样的事情来捕获你的错误。

this.myService.checkStatus
  .pipe(
     catchError(error) => // do something with it)
   )
  .subscribe(yourResponse => // handle your response)

You can do something like this, to catch your error.

this.myService.checkStatus
  .pipe(
     catchError(error) => // do something with it)
   )
  .subscribe(yourResponse => // handle your response)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文