我是新的可观察和实现常见的angular2服务来处理API调用并使用请求包装身份验证令牌。
为此我使用https://gist.github.com/chandermani/9166abe6e6608a31f471
现在处理401错误我已经改变了一点。
private _request(method: RequestMethod, url: string, body?: any, options?: RequestOptionsArgs): Rx.Observable<Response> {
let requestOptions = new RequestOptions(Object.assign({
method: method,
url: url,
body: body
}, options));
if (!requestOptions.headers) {
requestOptions.headers = new Headers();
}
requestOptions.headers.set('Authorization', this._buildAuthHeader());
return Rx.Observable.create((observer) => {
this.process.next(Action.QueryStart);
this._http.request(new Request(requestOptions))
.map(res => res.json())
.finally(() => {
this.process.next(Action.QueryStop);
}).catch((error) => {
if (error.status === 401) {
this.renewToken().subscribe((res) => { }, (err) => { }, () => {
requestOptions.headers.set('Authorization', this._buildAuthHeader());
this._http.request(new Request(requestOptions))
.map(res => res.json())
.subscribe(
(res) => {
observer.next(res);
observer.complete();
},
(err) => {
},
() => {
});
});
} else {
observer.next();
observer.complete();
return Rx.Observable.timer(1);
}
})
.subscribe(
(res) => {
observer.next(res);
observer.complete();
},
(err) => {
switch (err.status) {
case 401:
// intercept 401
this.authFailed.next(err);
observer.error(err);
break;
default:
observer.error(err);
break;
}
});
});
}
按预期工作正常。在401上,它刷新令牌,然后执行API调用并从API返回结果。但它在控制台中出错。
Cannot read property 'Symbol(Symbol.iterator)' of undefined
TypeError: Cannot read property 'Symbol(Symbol.iterator)' of undefined
at Object.subscribeToResult (eval at <anonymous> (http://localhost:8080/js/vendor.js:48:1), <anonymous>:45:27)
at CatchSubscriber.error (eval at <anonymous> (http://localhost:8080/js/vendor.js:1921:1), <anonymous>:60:33)
at FinallySubscriber.Subscriber._error (eval at <anonymous> (http://localhost:8080/js/vendor.js:20:1), <anonymous>:128:26)
at FinallySubscriber.Subscriber.error (eval at <anonymous> (http://localhost:8080/js/vendor.js:20:1), <anonymous>:102:18)
at MapSubscriber.Subscriber._error (eval at <anonymous> (http://localhost:8080/js/vendor.js:20:1), <anonymous>:128:26)
at MapSubscriber.Subscriber.error (eval at <anonymous> (http://localhost:8080/js/vendor.js:20:1), <anonymous>:102:18)
at XMLHttpRequest.onLoad (eval at <anonymous> (http://localhost:8080/js/vendor.js:1634:1), <anonymous>:88:34)
at ZoneDelegate.invokeTask (eval at <anonymous> (http://localhost:8080/js/polyfills.js:759:1), <anonymous>:236:37)
at Object.NgZoneImpl.inner.inner.fork.onInvokeTask (eval at <anonymous> (http://localhost:8080/js/vendor.js:2622:1), <anonymous>:35:37)
at ZoneDelegate.invokeTask (eval at <anonymous> (http://localhost:8080/js/polyfills.js:759:1), <anonymous>:235:42)
at Zone.runTask (eval at <anonymous> (http://localhost:8080/js/polyfills.js:759:1), <anonymous>:136:47)
at XMLHttpRequest.ZoneTask.invoke (eval at <anonymous> (http://localhost:8080/js/polyfills.js:759:1), <anonymous>:304:33)
请帮助我解决问题或如何追踪它。
答案 0 :(得分:0)
你可以在订阅点处理它。
observable$.subscribe(response => {
//success
},
response => {
//error
});
请参阅: