ngrx effects error handling

前端 未结 3 746
迷失自我
迷失自我 2020-12-01 14:20

I have a very basic question concerning @ngrx effects: How to ignore an error that happens during the execution of an effect such that it doesn\'t affect future effect execu

3条回答
  •  南笙
    南笙 (楼主)
    2020-12-01 15:00

    The @Effect stream is completing when the error occurs, preventing any further actions.

    The solution is to switch to a disposable stream. If an error occurs within the disposable stream it's okay, as the main @Effect stream always stays alive, and future actions continue to execute.

    @Effect()
    login$ = this.actions$
        .ofType('LOGIN')
        .switchMap(action => {
    
            // This is the disposable stream!
            // Errors can safely occur in here without killing the original stream
    
            return Rx.Observable.of(action)
                .map(action => {
                    // Code here that throws an error
                })
                .catch(error => {
                    // You could also return an 'Error' action here instead
                    return Observable.empty();
                });
    
        });
    

    More info on this technique in this blog post: The Quest for Meatballs: Continue RxJS Streams When Errors Occur

提交回复
热议问题