从 catchError 捕获错误 - http.post
capture error from catchError - http.post
正在调用 submitUser 的组件
this.someservice.submitUser(postData).subscribe((data) => {
this.viewUsers();
}, (err) => {
console.log('error in the component', err);
});
这是带有submitUser功能的服务文件
public submitUser(reqBody ) {
return this.httpService.post('roles', reqBody, '/business/create')
.pipe(
catchError(
this.httpService.handleError())
);
}
这里是 httpService Post 和 handleError 方法
public post<JSON>(url: string, body: any, param?: string, options?: IRequestOptions): Observable<JSON> {
return this.intercept(this.http.post<JSON>(this.getURLFromMethodName(url, param), body, this.requestOptions(options)));
}
handleError<T> (operation = 'operation', result?: T) {
return (error: any): Observable<T> => {
// TODO: send the error to remote logging infrastructure
console.error('error from httpclient', error); // log to console instead
throw throwError(new Error(error));
};
}
handleError a 显示控制台错误,我正在尝试 return/capture 我在 service.ts
中的 submitUser 函数中出现此错误
我该怎么做?感谢任何输入,谢谢
您的 handleError()
方法 return 是一个 error observable
以及将错误记录到控制台。
当发生某些错误时,catchError
运算符会接受该错误并将其提供给 handleError()
,后者又 return 是一个可观察到的错误。
案例 1:返回错误
如果您需要将此错误传递给订阅者,则无需执行任何操作。 catchError
接线员已经在为您处理。
使用相同的代码,假设某个组件正在使用您的服务,那么您可以编写
someService.submitUser().subscribe((res) => {
\ handle success
}, (err) => {
console.error(err); // Will print the error
});
每当错误发生时,catchError 将 return 可观察到的错误返回给它的订阅者,它将进入观察者的错误函数,如上面的代码片段所示。
案例 2:处理错误
catchError
运算符接受一个以 error
作为参数的函数。如果您 return 在其中使用另一个可观察对象而不是抛出错误,订阅者将不会知道错误已经发生,观察者的成功函数将执行。
// Inside the service
public submitUser(reqBody ) {
return this.httpService.post('roles', reqBody, '/business/create')
.pipe(
catchError((err) => of([1,2,3]));
}
// Inside the component consuming the service
someService.submitUser().subscribe((res) => {
console.log(res) // Will print [1,2,3]
}, (err) => {
\ handle error
});
正在调用 submitUser 的组件
this.someservice.submitUser(postData).subscribe((data) => {
this.viewUsers();
}, (err) => {
console.log('error in the component', err);
});
这是带有submitUser功能的服务文件
public submitUser(reqBody ) {
return this.httpService.post('roles', reqBody, '/business/create')
.pipe(
catchError(
this.httpService.handleError())
);
}
这里是 httpService Post 和 handleError 方法
public post<JSON>(url: string, body: any, param?: string, options?: IRequestOptions): Observable<JSON> {
return this.intercept(this.http.post<JSON>(this.getURLFromMethodName(url, param), body, this.requestOptions(options)));
}
handleError<T> (operation = 'operation', result?: T) {
return (error: any): Observable<T> => {
// TODO: send the error to remote logging infrastructure
console.error('error from httpclient', error); // log to console instead
throw throwError(new Error(error));
};
}
handleError a 显示控制台错误,我正在尝试 return/capture 我在 service.ts
中的 submitUser 函数中出现此错误我该怎么做?感谢任何输入,谢谢
您的 handleError()
方法 return 是一个 error observable
以及将错误记录到控制台。
当发生某些错误时,catchError
运算符会接受该错误并将其提供给 handleError()
,后者又 return 是一个可观察到的错误。
案例 1:返回错误
如果您需要将此错误传递给订阅者,则无需执行任何操作。 catchError
接线员已经在为您处理。
使用相同的代码,假设某个组件正在使用您的服务,那么您可以编写
someService.submitUser().subscribe((res) => {
\ handle success
}, (err) => {
console.error(err); // Will print the error
});
每当错误发生时,catchError 将 return 可观察到的错误返回给它的订阅者,它将进入观察者的错误函数,如上面的代码片段所示。
案例 2:处理错误
catchError
运算符接受一个以 error
作为参数的函数。如果您 return 在其中使用另一个可观察对象而不是抛出错误,订阅者将不会知道错误已经发生,观察者的成功函数将执行。
// Inside the service
public submitUser(reqBody ) {
return this.httpService.post('roles', reqBody, '/business/create')
.pipe(
catchError((err) => of([1,2,3]));
}
// Inside the component consuming the service
someService.submitUser().subscribe((res) => {
console.log(res) // Will print [1,2,3]
}, (err) => {
\ handle error
});