Angular http请求可观察错误没有捕获错误?
Angular http request observable error does not catch error?
Angular 6 使用 HttpClient。目前没有实现拦截器。
我有一个管理服务,我正在其中对 MSGraph 进行 http 调用。在这种情况下,我收到错误 400 错误请求,我将其发送到通知处理程序。
我已经滑入那个 console.log 以确保错误块完全触发,另一个 cnosole.log 以查看从服务器返回的 'data' 是否是一个错误。
None 的 console.log 触发,但全局错误处理程序仍然在控制台中显示 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);
}
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';
Angular 6 使用 HttpClient。目前没有实现拦截器。
我有一个管理服务,我正在其中对 MSGraph 进行 http 调用。在这种情况下,我收到错误 400 错误请求,我将其发送到通知处理程序。
我已经滑入那个 console.log 以确保错误块完全触发,另一个 cnosole.log 以查看从服务器返回的 'data' 是否是一个错误。
None 的 console.log 触发,但全局错误处理程序仍然在控制台中显示 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);
}
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';