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 execution?
My situation is as follows: I have an action (LOGIN) and an effect listening to that action. If an error happens inside this effect, I want to ignore it. When LOGIN is dispatched a second time after this error, the effect should be executed a second time.
My first attempt to do this was:
@Effect() login$ = this.actions$ .ofType('LOGIN') .flatMap(async () => { console.debug('LOGIN'); // throw an error let x = [];x[0](); }) .catch(err => { console.error('Error at login', err); return Observable.empty(); });
Dispatching LOGIN the first time throws and catches the error, as expected. However, if I dispatch LOGIN a second time afterwards, nothing happens; the effect is not executed.
Therefore I tried the following:
.catch(err => { return this.login$; });
, but this results in an endless loop... Do you know how to catch the error without preventing effect execution afterwards?
Most effects are straightforward: they receive a triggering action, perform a side effect, and return an Observable stream of another action which indicates the result is ready. NgRx effects will then automatically dispatch that action to trigger the reducers and perform a state change.
You need to run this command npm install @ngrx/effects — save to install required dependencies. You have to define the actual services to make the API calls. Finally, you need to register all the effects with the EffectsModules and import this module into your main module or feature module.
In essence, learning NgRx is not very easy, but it is not as challenging as learning an entire new framework like Angular itself; and definitely worth the time invested in it. Once one of the concepts is mastered, the next is much easier to understand.
The ngrx
infrastructure subscribes to the effect via the provider imported into the app's NgModule
using EffectsModule.run
.
When the observable errors and catch
returns a empty obervable, the composed observable completes and its subscribers are unsubscribed - that's part of the Observable Contract. And that's the reason you see no further handling of LOGIN
actions in your effect. The effect is unsubscribed on completion and the ngrx
infrastructure does not re-subscribe.
Typically, you would have the error handling inside the flatMap
(now called mergeMap
):
import { Actions, Effect, toPayload } from "@ngrx/effects"; @Effect() login$ = this.actions$ .ofType('LOGIN') .map(toPayload) .flatMap(payload => Observable .from(Promise.reject('Boom!')) .catch(error => { console.error('Error at login', error); return Observable.empty(); }) });
The catch
composed into the inner observable will see an empty observable flattened/merged into the effect, so no action will be emitted.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With