在 Angular 6+ httpClient 中,哪个响应状态代码转到 catchError?
Which response status code goes to catchError in Angular 6+ httpClient?
在Angular6+ httpClient中,一个请求可以配置为获取整个响应。
可观察到的响应可以通过管道传输到 map
和 catchError
运算符中。
执行什么时候通过 map
运算符,什么时候到 catchError
?
是否取决于响应状态码?
例如,如果 response.status === 200
则转到 map
,否则转到 catchError
?
如果不仅状态 200 转到 map
,还有哪个?
哪些状态会进入 catchError
?
getData(): Observable<[]> {
return this.http.get(this.apiUrl, {observe: 'response'}).pipe(
map((response: HttpResponse<any>) => {
return response.status === 200;
}),
catchError((errorResponse: HttpErrorResponse) =>
// which value may be logged here?
console.log(errorResponse.status);
of(false);
));
}
4xx 和 5xx 状态代码是错误。其他的都成功了。
在Angular6+ httpClient中,一个请求可以配置为获取整个响应。
可观察到的响应可以通过管道传输到 map
和 catchError
运算符中。
执行什么时候通过 map
运算符,什么时候到 catchError
?
是否取决于响应状态码?
例如,如果 response.status === 200
则转到 map
,否则转到 catchError
?
如果不仅状态 200 转到 map
,还有哪个?
哪些状态会进入 catchError
?
getData(): Observable<[]> {
return this.http.get(this.apiUrl, {observe: 'response'}).pipe(
map((response: HttpResponse<any>) => {
return response.status === 200;
}),
catchError((errorResponse: HttpErrorResponse) =>
// which value may be logged here?
console.log(errorResponse.status);
of(false);
));
}
4xx 和 5xx 状态代码是错误。其他的都成功了。