ValueChanges stops working as soon as an error occurs in Rx.Observable

I am following some Rx.Observable tutorials from an Angular 2 application and using the system autocomplete style.

When I enter into the field, the valueChanges event is fired from Angular 2 FormControl.

This is tied to an Observable, which makes an HTTP request against the JSON endpoint.

When the endpoint returns 404, the valueChanges event stops working.

I see an error in my subscription method, but am not quite sure what the best way to recover and continue.

I am also a bit confused why the KeyUp or ValueChange event will stop firing.

Change sample value - observed chain

this.userNameControl
    .valueChanges
    .do(r => {
            // As soon as a 404 status is thrown from getGitHuybUser$, all value change (keyup) events stop working
            console.log
        }
    )
    .switchMap(userName => {
        return this.getGitHubUser$(userName);
    })
    .catch(err => {
        return Observable.of(err)
    })
    .subscribe(v => {
            console.log(v);
        },
        (err) => {
            // As soon ass there is a 404 status, I end up here
            console.log(err);
        },
        () => {
            console.log('Complete');
        });

getGitHubUser$(username) {
    return this.http
        .get(`https://api.github.com/users/${username}`)
}

HTML form management

<input type="text" [value]="userName" [formControl]="userNameControl" />

I tried to return Observable.empty () and Observable.never () in catch

.catch(err => {
    // Observable.of(err)
    // return Observable.empty();
    return Observable.never();
})

, subscribe, complete, , Changes .

+4
1

, .catch(), .switchMap(), this.getGitHubUser$(userName)

this.userNameControl
    .valueChanges
    .do(r => {
            // As soon as a 404 status is thrown from getGitHuybUser$, all value change (keyup) events stop working
            console.log(r);
        }
    )
    .switchMap(userName => {
        console.log('GET GIT HUB USER');

        return this.getGitHubUser$(userName)
            .catch(err => {
                console.log('CATCH INNER');
                console.log(err);
                return Observable.of(err)
            })
    })
    // .catch(err => {
    //     // THIS CODE IS NOT NEEDED
    //     console.log('CATCH');
    //     console.log(err);
    //     return Observable.never(); // Observable.of(err)
    // })
    .subscribe(v => {
            console.log('SUCCESS');
            console.log(v);
        },
        (err) => {
            console.log('ERROR');
            console.log(err);
        },
        () => {
            console.log('COMPLETE');
        });
+2

Source: https://habr.com/ru/post/1664487/


All Articles