Mic*_*elB 4 observable rxjs angular angular-httpclient
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);
}
Run Code Online (Sandbox Code Playgroud)
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));
Run Code Online (Sandbox Code Playgroud)
因此,您可以使用pipe调用catchError,它可以正确处理服务器错误。
要使用catchError,您需要先将其导入,如下所示:
import { catchError } from 'rxjs/operators';
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3600 次 |
| 最近记录: |