首页 文章

Angular http请求可观察错误没有捕获错误?

提问于
浏览
2

Angular 6使用HttpClient . 目前没有实施拦截器 .

我有一个管理服务,我正在对MSGraph进行http调用 . 在这种情况下,我收到错误400错误请求,我发送给通知处理程序 .

我已经滑入了console.log,以确保错误块正在触发,另一个cnosole.log,以查看从服务器返回的“数据”是否是错误 .

没有console.logs触发,但全局错误处理程序仍在控制台中显示400错误请求响应 .

我希望能够使用此响应并显示用户友好的消息 .

码:

this.http.get(this.GRAPH_ROOT_URL + this.APP_ID, { headers }).subscribe(data => {
  this.notification.done(data);
  console.log(data);
}), error => {
  this.notification.error(`Error getting users from Microsoft GRAPH API. Reason: ${error}`)
  console.log("this is observable error", error);
}

1 回答

  • 1

    Angular 6使用RxJS 6,它有许多变化 . 假设你正在使用的是这个我会用的:

    这是我会用的:

    this.http.get(this.GRAPH_ROOT_URL + this.APP_ID, { headers })
        .pipe(catchError(err => {
          this.alert.error('There was an error getting data');
          return throwError(err);
        })).subscribe(data => this.notification.done(data));
    

    所以你使用 pipe 来调用 catchError ,它可以正确处理服务器错误 .

    要使用 catchError ,您需要先导入它,如下所示:

    import { catchError } from 'rxjs/operators';
    

相关问题