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

Capture errors thrown in action creators #205

Closed
PsychoLlama opened this issue Nov 12, 2020 · 1 comment
Closed

Capture errors thrown in action creators #205

PsychoLlama opened this issue Nov 12, 2020 · 1 comment

Comments

@PsychoLlama
Copy link
Collaborator

PsychoLlama commented Nov 12, 2020

I need error reducers to intercept error events, not just those gracefully returned by failure(...). This means adding a catch statement on the effect, but as I outlined over here, I can't just swallow every error. That means conditionally re-throwing after dispatch is done.

The only way that's possible is through async generator functions.

async function* createAction(type, effect) {
  try {
    yield { type, payload: effect() }
  } catch (error) {
    yield { type, error: true, payload: error }
    if (isUnknown(error)) throw error
  }
}
@PsychoLlama PsychoLlama changed the title Replace synchronous action creator with async generator Capture errors thrown in action creators Nov 12, 2020
@PsychoLlama
Copy link
Collaborator Author

Hmm, speed bump. I had assumed the only noticeable effect would be if you call the action directly (without a redux dispatch), but while implementing the change, I realized the return value of dispatch changes to a promise. This would implicitly require all synchronous actions to return promises. I'm not comfortable with that tradeoff.

I'm pursuing synchronous iterators in #210.

PsychoLlama added a commit that referenced this issue Nov 12, 2020
I'm using the new synchronous generator feature exposed by PR #211. This
allows me to detect and report errors without swallowing them.

Breaking change: all errors trigger the `handleAction.error(...)` code
path, not just `failure(...)` return values. This also means it's much
harder to test action creators in isolation (as evidenced by the failing
tests in the examples/ directory).

Issue #205
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

1 participant