【发布时间】:2018-09-19 07:46:06
【问题描述】:
我有一个全局 HttpInterceptor,它带有一个处理 HttpErrorResponse 的 catch 块。但我的要求是,当服务进行 http 调用并且还有错误处理程序时,我希望服务上的错误处理程序首先关闭。如果此服务上没有错误处理程序,那么我希望全局 HttpInterceptor 错误处理程序来处理它。
示例代码:
Http 拦截器:
@Injectable()
export class ErrorHttpInterceptor implements HttpInterceptor {
constructor(private notificationService: NotificationService) {}
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(req)
.catch(
error => {
if (error instanceof HttpErrorResponse) {
this.notificationService.error('Error', 'Error in handling Http request');
}
return Observable.empty<HttpEvent<any>>();
}
);
}
}
以及服务调用:
updateUser(id, data) {
return this.http
.patch(`${API.userUrl}/${id}`, {data: data})
.subscribe(
() => console.log('success'),
(err) => this.notificationService.error('custom code to handle this')
);
}
在这种情况下,ErrorHttpInterceptor 会发出通知,然后 userService 错误处理也会发出错误通知。
但在我的用例中,我希望 ErrorHttpIntercetor 仅在底层订阅未处理错误时处理错误。有没有办法做到这一点?
【问题讨论】:
标签: angular angular-http-interceptors angular-httpclient