platform icon indicating copy to clipboard operation
platform copied to clipboard

ComponentStore updater errors are not catchable

Open kjelko opened this issue 4 years ago • 0 comments

Minimal reproduction of the bug/regression with instructions:

https://stackblitz.com/edit/angular-ivy-tgtbh9?devtoolsheight=33&file=src/app/hello.component.ts

Expected behavior:

I expect the try/catch block to catch the error that is thrown in the ComponentStore updater.

Versions of NgRx, Angular, Node, affected browser(s) and operating system(s):

This should all be captured in Stackblitz.

Other information:

A few notes:

  • I suspect this might have something to do with the recent RxJS SuperGrossMode deprecation, which throws unhandled errors in a setTimeout.
  • When I originally designed the ComponentStore, I was using a BehaviorSubject and could thus call updater functions directly, ie: this.state$.next(updaterFn(this.state$.value, updaterArg));. There was a lot of async logic that was applied to updaters that makes it a little less obvious how to fix this issue. I'm wondering if moving the updaterFn call to a map (or maybe even a concatMap w/ nested try/catch) before the .subscribe() in the updater might fix this.
  • I am seemingly able to make things work by manually sending an error through on stateSubject$ (see commented line in my Stackblitz demo). I'm not sure this is really intentional though. It seems to be hitting the initializationError re-throw block, which doesn't necessarily seem appropriate (since this isn't an initialization error).
  • I also see that updater errors (or at least the initialization errors mentioned above) are pushed onto the stateSubject$ itself, which isn't a decision I'm familiar with. It doesn't seem like this captures updater errors.

kjelko avatar Dec 20 '21 22:12 kjelko