简体   繁体   English

服务器不响应时处理错误

[英]Handle error when server does not respond

My application is build using Angular 2. A typical pattern I use is seen below. 我的应用程序是使用Angular 2构建的。我使用的典型模式如下所示。 If there is a server error I do a number of things depending on the needs. 如果发生服务器错误,我会根据需要执行很多操作。 In the below example I used a comment to indicate a redirect to an error page. 在下面的示例中,我使用注释来指示重定向到错误页面。

However occasionally something goes wrong and the server does not respond causing the front end to remain in a state of pending with the spinner spinning. 但是,有时会出现问题,并且服务器无法响应,从而导致前端在旋转器旋转时仍处于挂起状态。 I want to know how this kind of situation is dealt with. 我想知道如何处理这种情况。 Is there a common practice for this? 有没有一种惯例? Is there a way to redirect at some point in a elegant manner? 有没有办法在某个时候以一种优雅的方式重定向? I feel like I could add a timeout to trigger at a certain time but that really seems like a hack. 我觉得我可以在某个特定的时间添加超时来触发,但这确实像是一个hack。

private getSites() {
  this.spinner.show();
  this.sitesService.getSites(1)
    .subscribe(
      _data => {
        this.sites = _data;
        this.spinner.hide();
        this.loading = false;
      },
      _error => {
        // Redirect to < something went wrong >
        this.spinner.hide();
      }
    );

}

Based on the responses I was able to learn various ways to deal with this situation. 根据答复,我能够学习各种方法来处理这种情况。 The one I liked best was using timeout operator. 我最喜欢的一个是使用超时运算符。 Thanks everyone. 感谢大家。

private getSites() {
  this.spinner.show();
  this.sitesService.getSites(1)
    .timeout(6000)
    .subscribe(
      _data => {
        this.sites = _data;
        this.spinner.hide();
        this.loading = false;
      },
      _error => {
        // Redirect to < something went wrong >
        this.spinner.hide();
      }
    );

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM