我使用一个HTTP API,通过一个JSON返回应用程序级错误,其中status =='error'。Angular 4:如何正确处理应用程序级http错误为可观察错误?
在我的注册服务,我这样做:
return this.userService
.create(user)
.map(
json => {
console.log(json);
if (json.status=='error') {
console.log('error return throw');
return Observable.throw('Credentials mismatch or not existing');
}
const user = json.data as User;
return user;
},
err => {
console.log(err);
}
);
在我singUp.component,我这样做:
onSubmit() {
// blahblah
this.si
.signUp(this.credentials.emailAddress, this.credentials.password, this.credentials.zipCode)
.subscribe(
user => {
console.log(user);
console.log(`SignUpComponent.onSubmit() found user.userId ${user.userId}`);
this.router.navigate(['signin']);
},
err => {
this.errorMessage = `*** SignUpComponent: Error while signing up {err}`;
console.error('*** SignUpComponent: Error while signing up', err);
this.resume();
//this.router.navigate(['signup']);
});
this.ngOnChanges();
}
不幸的是,当服务返回错误(与可观测.throw())时,组件不会触发err闭包,而是会将用户参数传递Observable.throw作为用户闭包。
我希望我有err关闭被触发。
我缺少什么?
更新:这是我所得到的:
[Log] SigninService.signUp, zipCode=
[Log] {data: null, status: "error", message: "Error user create: - missing id API usage v1b3: Tu…ate\"}↵post: []↵.↵", ws: "v1b3: Tuesday, August 25th 2017 20h20 @ Tanger"}
[Log] error return throw
[Log] ErrorObservable {_isScalar: false, error: "Credentials mismatch or not existing", scheduler: undefined, _subscribe: function, …}
[Log] SignUpComponent.onSubmit() found user.userId undefined
什么是你'.MAP()'d卷板机? – Aravind
它会查找API状态以确保API返回全新用户(当status =='success'时)。如果状态等于“错误”(代表用户帐户未被创建),我希望我为订户生成错误。 –
这工作? – Aravind