0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

redux-observableのEpicのエラーハンドリングでactionを返す

Posted at

普通に書こうとすると失敗する

catchErrorオペレータを使い、ofでくくったActionを返します

サンプルコード(失敗します)
const sampleEpic = action$ =>
  action$.pipe(
    filter(sampleAction.match),
    tap(() => {
      throw new Error('oops!'); // エラー発生
    }),
    catchError(error =>
      of(errorHandlingAction(error.message)) // Actionを返す
    )
  );

しかし、一回catchErrorをしたのを最後に、このEpicは死んでしまいました:cry:

しばらくハマったのですが、redux-observableのcatchErrorでactionを返すと、rootEpicがそのEpicの購読をやめてしまうようです:thinking:

解決策

issueに解決策がありました。

catchError((error, caught) => merge(
	of({ type: 'EPIC_FAILURE', error }),
	caught,
)),

なるほど、一緒にcaughtもすればいいんですね:hugging:

サンプルコード(修正版)
const sampleEpic = action$ =>
  action$.pipe(
    filter(sampleAction.match),
    tap(() => {
      throw new Error('oops!'); // エラー発生
    }),
    catchError((error, caught) =>
      merge(of(errorHandlingAction(error.message)), caught) // caughtする!
    )
  );
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?