【发布时间】:2019-09-17 14:15:11
【问题描述】:
我有一个处理 http 请求的 http 服务。它有一个处理程序,通过在屏幕上弹出一个模式来处理错误。
我还有一个拦截器,它可以捕获插入授权令牌的请求,以备不时之需。令牌来自请求另一个服务。
问题是,如果需要授权请求,但失败了,即使第二个请求从未发送,模态错误也会显示两次。
如果我认为我处理 Observable 流的方式有问题。有人可以帮忙吗?
/// http service
request(url, body, headers) {
return this.httpClient.post(url, body, headers).pipe(
catchError(() => this.handleError())
);
}
handleError() {
this.showModal();
return throwError('Theres been an error.');
}
/// interceptor
intercept(request, next) {
const authorization = request.headers.get('Authorization');
if (authorization) {
return this.httpService.request(url, body, headers).pipe(
exhaustMap(token => {
const newRequest = request.clone({
headers: request.headers.set(
'Authorization',
token
)
});
return next.handle(newRequest);
})
);
}
return next.handle(request);
}
【问题讨论】:
标签: javascript ionic-framework rxjs