在ngrx效果中不捕获错误可以吗?

huangapple go评论52阅读模式
英文:

Is it okay not to catchError in ngrx effect

问题

以下是您要翻译的代码部分:

  fetchData$ = createEffect(() => {
    return this.actions$.pipe(
      ofType(fetchData),
      switchMap(() =>
        this.repository.fetchData().pipe(
          map(data => fetchDataSuccess({ data })),
          catchError(error => of(fetchDataFail({ error })))
        )
      )
    );
  });
  fetchData() {
    return this.httpClient.get(`https://api.com/getData`).pipe(this.handleError());
  }

  handleError(err: HttpErrorResponse) {
    return pipe(
      catchError(err => {
        this.notification.create('Oops something went wrong');
        throw err;
      })
    );
  }
  fetchData$ = createEffect(() => {
    return this.actions$.pipe(
      ofType(fetchData),
      switchMap(() =>
        this.repository.fetchData().pipe(
          map(data => fetchDataSuccess({ data }))
        )
      )
    );
  });

请注意,我已将HTML实体 > 翻译为正常的 > 符号以确保代码可读性。

英文:

Let's assume that I have a following code:

  fetchData$ = createEffect(() => {
    return this.actions$.pipe(
      ofType(fetchData),
      switchMap(() =>
        this.repository.fetchData().pipe(
          map(data => fetchDataSuccess({ data })),
          catchError(error => of(fetchDataFail({ error })))
        )
      )
    );
  });

In the system I've built, there is a custom handler bind to every http call, witch handles error. In a nutshell, it looks like this:

  fetchData() {
    return this.httpClient.get(`https://api.com/getData`).pipe(this.handleError());
  }

  handleError(err: HttpErrorResponse) {
    return pipe(
      catchError(err => {
        this.notification.create('Oops something went wrong');
        throw err;
      })
    );
  }

Every time when http error occurs, there is a notification created and error is printed to the console (then read by a tool for error reporting). My question is: can I safely omit catchError in effect, if I'm not doing anything with this action there, besides actually dispatching it? Error is handled somewhere else, notification is displayed to the user, this action is not handled in reducer. If I remove this, won't this cause any performance / memory issues? I understand that this will cause error to be basically thrown and not be caught anywhere
In the result effect will look like this:

  fetchData$ = createEffect(() => {
    return this.actions$.pipe(
      ofType(fetchData),
      switchMap(() =>
        this.repository.fetchData().pipe(
          map(data => fetchDataSuccess({ data }))
        )
      )
    );
  });

I've tried looking for some articles about it, but none of them mentioned this kind of situation. When I changed the effect it works perfectly well.

答案1

得分: 1

在以前的 NgRx 版本中,这是一个问题。
如果在 effect 中抛出错误,那么该 effect 将会取消订阅。

目前的答案是肯定和否。
该 effect 有一个内建的错误处理器,但只适用于前 10 个错误。
如果你想要扩展它,可以通过创建自己的错误处理器来实现。

{
  provide: EFFECTS_ERROR_HANDLER,
  useValue: defaultEffectsErrorHandler,
},

https://github.com/ngrx/platform/blob/master/modules/effects/src/effects_error_handler.ts

英文:

In previous versions of NgRx this WAS a problem.
If an error was thrown in the effect, than the effect would unsubscribe.

Currently the answer is yes and no.
The effect has a built-in error handler, but only for the first 10 errors.
You can extend this if you want by creating your own error handler.

   {
          provide: EFFECTS_ERROR_HANDLER,
          useValue: defaultEffectsErrorHandler,
        },

https://github.com/ngrx/platform/blob/master/modules/effects/src/effects_error_handler.ts

huangapple
  • 本文由 发表于 2023年2月14日 22:29:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/75449275.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定