代码之家  ›  专栏  ›  技术社区  ›  Stéphane de Luca

角度4:如何将应用程序级http错误作为可观察错误正确处理?

  •  1
  • Stéphane de Luca  · 技术社区  · 7 年前

        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);
                }
            );
    

    在我的歌唱中。组件,我这样做:

        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();
        }
    

    我希望能触发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
    
    3 回复  |  直到 7 年前
        1
  •  1
  •   DAG    7 年前

    您正在处理用户回调中的异常,在这种情况下,这意味着它已成功执行 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)

        2
  •  0
  •   apeshev    7 年前

    throw 'Credentials mismatch or not existing' 没有 回来 可观察的 部分。在执行可观察对象期间产生的任何错误都将作为当前 Observable

    可观察的 这不是你想要的。

    error

        3
  •  0
  •   Stéphane de Luca    7 年前

    我最终提出了以下解决方案,扩展了以下事实:1)使用throw new Error()我仍然可以返回一个可观察的,2)当Error抛出subscribe completion参数时,不触发,需要使用finally()调用,如下所示:

    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();
        }