ngneat / effects

🪄 A framework-agnostic RxJS effects implementation
https://www.netbasal.com
MIT License
63 stars 11 forks source link

Error handling with effect #30

Closed feitzi closed 2 years ago

feitzi commented 2 years ago

Which @ngneat/effects-* package(s) are the source of the bug?

effects-ng

Is this a regression?

No

Description

If an effect returns an unhandled exception, it can not be started again. For me its seems like a bug, or some kind of missing documentation to handle errors. It's possible to use catchError, but there should be a more generic way to solve that problem?

From my submitted sample: image

Please provide a link to a minimal reproduction of the bug

https://codesandbox.io/s/elfeffectserrorhandling-oo7qvf?file=/src/app/app.component.ts

Please provide the exception or error you saw

No response

Please provide the environment you discovered this bug in

No response

Anything else?

No response

Do you want to create a pull request?

Yes

NetanelBasal commented 2 years ago

Use tapResult. See readme.

feitzi commented 2 years ago

I updated the sample, and it doesn't work like described in the readme. After the error occurs, the effect cannot used anymore.

 sampleEffect = this.createEffectFn(($: Observable<number>) =>
    $.pipe(
      switchMap(this.fakeApiCall),
      tapResult(
        (x) => console.log("Further processing of:", x),
        (error) => console.error(error)
      )
    )
  );

Can you please adapt my minimal sandbox example to show a working error handling? Maybe then I can create a PR with documentation on this topic for other people.

NetanelBasal commented 2 years ago

It should be in the inner observable:

  sampleEffect = this.createEffectFn(($: Observable<number>) =>
    $.pipe(
      switchMap(() => { 
        return from(fetch('https://not-exists')).pipe(
          tapResult(
            (x) => console.log("Further processing of:", x),
            (error) => console.error(error)
          )
        )
      }),
    )
  );
feitzi commented 2 years ago

Ok, then the documentation is wrong, I created a PR (#31). But the problem is still there. You would have to give a separate tapResult call for each call. Is there no possibility to do this with a global error handler?

NetanelBasal commented 2 years ago

No, because it depends on the stream.