2017-08-26 25 views
1

我使用一个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 
+0

什么是你'.MAP()'d卷板机? – Aravind

+0

它会查找API状态以确保API返回全新用户(当status =='success'时)。如果状态等于“错误”(代表用户帐户未被创建),我希望我为订户生成错误。 –

+0

这工作? – Aravind

回答

1

您正在处理用户回调内的异常,这意味着它已被成功执行,在这种情况下,Observable.throws不会真正作为例外。为了实现这一点,您必须使用throw new Error("Credentials mismatch or not existing")必须替换return Observable.throw("Credentials mismatch or not existing");请看,看看这个:How to throw error from RxJS map operator (angular)

+0

感谢@dag让我走上正轨。修改您的答案为:抛出新的错误(“证书不匹配或不存在”);必须替换返回Observable.thow(“证书不匹配或不存在”);这样我才能接受它。 –

0

为了避免您遇到的问题,我想你只需要做throw 'Credentials mismatch or not existing'没有回报可观测部分。在执行observable期间产生的任何错误都会在当前的Observable上传播为错误。

目前,你只是用Observable替换json响应,这不是你想要的。

理想情况下,服务器应该返回错误响应而不是带有错误消息的json,并且会触发您在订阅中的error处理程序。

+0

中加入了日志,这是我第一次尝试之前尝试返回Observable.throw()在desesparate移动。 –

0

我终于想出了以下解决方案,扩展了1)用throw new Error()我仍然可以返回一个Observable和2)当错误抛出subscribe完成参数时不触发,一个需要使用的最后()调用如下:

注册服务:

create(user: User): Observable<User> { 
    const wsUrl = `${this.wsApi}m=create&id=${user.id}`; // URL to web api 

    return this.http.post(wsUrl, JSON.stringify(user), { headers: this.headers }) 
    .retry(3) 
    .map(res => { 
     const json = res.json() as any; 

     if (json.status=='error') throw new Error(json.message); 

     return json.data as User; 
    }); 
} 

注册组件:

onSubmit() { 
     // blahblah 

     this.progressing = true; 

     this.si 
     .signUp(this.credentials.emailAddress, this.credentials.password, this.credentials.zipCode) 
     .finally(() => setTimeout(() => this.progressing = false, 1000)) 
     .subscribe(
      user => this.router.navigate(['signin']), 
      err => this.errorMessage = err 
     ); 

     this.ngOnChanges(); 
    }