Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

catchError in effects not working #63

Open
jmeinlschmidt opened this issue Jul 19, 2023 · 2 comments
Open

catchError in effects not working #63

jmeinlschmidt opened this issue Jul 19, 2023 · 2 comments

Comments

@jmeinlschmidt
Copy link

jmeinlschmidt commented Jul 19, 2023

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

effects-ng

Is this a regression?

No

Description

Operator catchError in effects is not working and after throwing an error, the effect stops emitting. Please see full example in stackblitz.

public myEffect = this.createEffectFn(($: Observable<string>) =>
  $.pipe(
    tap((no) => console.log('effect invocation', no)),
    map(() => {
      throw new Error('error here');
    }),
    catchError((e) => {
      console.log('catched', e);
      return of();
    })
  )
);

This is related to #34. I think this applies to effect functions as well, see createEffectFn implementation.

Please provide a link to a minimal reproduction of the bug

https://stackblitz.com/edit/stackblitz-starters-tvg994?file=src%2Fmain.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?

No

@stackblitz
Copy link

stackblitz bot commented Jul 19, 2023

Fix this issue in StackBlitz Codeflow Start a new pull request in StackBlitz Codeflow.

@dimi4e
Copy link

dimi4e commented Dec 22, 2023

I have the same issue, when try to make HTTP request

loadUsers$ = createEffect((actions) =>
        actions.pipe(
            ofType(loadUsers),
            switchMap(() => this.usersService.getAll()),
            catchError((err) => { notifyAboutError(err); return NEVER; }),
            tap(setUserList)
        )
    );

After an error occurs, the effect stops. One possible solution is to refrain from catching errors within the action pipe and instead catch them in the HTTP response stream, as illustrated below:

loadUsers$ = createEffect((actions) =>
        actions.pipe(
            ofType(loadUsers),
            switchMap(() => this.usersService.getAll().pipe(catchError(() => {
                 notifyAboutError(err);
                 return of(null);
            }))),
            tap((result) => {
                if (!result) return;
                setUserList(result);
            })
        )
    );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants