当API错误出现时,我正在尝试处理UI中的唯一约束错误。
我的服务类别如下:
import { catchError, map } from 'rxjs/operators';
import { Observable } from 'rxjs/Rx';
import { HttpErrorResponse } from '@angular/common/http';
createArticle(ArticlePayload: any) {
return this.http.post('/create', ArticlePayload, this.requestOptionHeaders).pipe(
map(response => response.json()),
catchError(err => this.errorHandler(err)));
}
public errorHandler(error: HttpErrorResponse) {
console.log(' errorHandler =========> ' + JSON.stringify(error));
return Observable.of(error);
}
我的组件如下:
createArticle(payload: any) {
this.myService.createArticle(payload).subscribe(
response => {
console.log(' response =>' + JSON.stringify(response));
// something else
}, (error) => {
console.log(' FAILED =>' + JSON.stringify(error));
this.notificationService.messageNotify('error', '', 'custom message needed here');
}
);
}
控制台日志:
errorHandler => {}
response => {}
我所缺少的。有人可以帮我吗? 我正在使用rxjs:“ ^ 6.2.2”版本