我有一个调用授权服务的登录服务,我需要捕获从api服务器返回的任何错误。
我目前在我的login.component中有这个:
loginUser() {
console.log(this.localUser.email);
console.log(this.localUser.password);
this.loginService.loginUser(this.localUser)
.subscribe(
token => {
console.log('token = ' + token)
this.token = token},
error => {
this.errorMessage = <any>error
console.log("err msg = " + this.errorMessage);
},
() => this.completeLogin());
}
这是我的登录服务:
loginUser(localUser: LocalUser) {
return this.authorizationService.loginUser(localUser);
}
这是我的身份验证服务:
loginUser(localUser: LocalUser): Observable<Token> {
var grantString: string = "grant_type=password&username=";
var passwordString : string = "&password="
var email = localUser.email;
var password = localUser.password;
var headers = new Headers();
headers.append('Content-Type', 'application/x-www-form-urlencoded');
var creds:string = grantString + email + passwordString + password;
return this.http.post(this.url, creds, { headers: headers })
.retry(2)
.timeout(20000, new Error('Timeout expired.'))
.delay(1000)
.map((response: Response) => <Token>response.json())
.catch(this.handleError);
}
handleError(error: any) {
let errorMsg = error.message || 'Server error';
console.error(errorMsg);
return Observable.throw(errorMsg);
}
问题是返回的错误消息不是服务器错误,而是: “err msg = TypeError:Object不支持属性或方法'throw'”
关于如何获取真实信息的任何想法?
答案 0 :(得分:4)
很可能你错过了rxjs'throw'static的导入。
如果您逐个导入rxjs运算符和静态函数,就可能发生这种情况。
//静力学 import'rxjs / add / observable / throw';
如需完整清单,请在此处查看:
https://github.com/ReactiveX/rxjs/blob/master/src/Rx.ts