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

Cancel args memorization if func throws exception #144

Merged
merged 3 commits into from
Jul 3, 2016

Conversation

zandroid
Copy link
Contributor

@zandroid zandroid commented Jul 2, 2016

The issue is when func throws exception the last args already saved and next calling of memoized func will return null instead of recalculating func again. If we save args after calling of func we save args only for "good" result and don't for exceptions.

The issue is when func throws exception the last args already saved and next calling of memoized func will return null instead of recalculating func again. If we save args after calling of func we save args only for "good" result and don't for exceptions.
@coveralls
Copy link

coveralls commented Jul 2, 2016

Coverage Status

Coverage remained the same at 100.0% when pulling 1c106a8 on zandroid:rethrow-exception into 9dd61bf on reactjs:master.

@codecov-io
Copy link

codecov-io commented Jul 2, 2016

Current coverage is 100%

Merging #144 into master will not change coverage

@@           master   #144   diff @@
====================================
  Files           1      1          
  Lines          13     13          
  Methods         0      0          
  Messages        0      0          
  Branches        0      0          
====================================
  Hits           13     13          
  Misses          0      0          
  Partials        0      0          

Powered by Codecov. Last updated by 9dd61bf...28873e8

@ellbee
Copy link
Collaborator

ellbee commented Jul 3, 2016

Hey @zandroid, would it be possible to add a test case that fails on the current version and passes with your fix please?

@zandroid
Copy link
Contributor Author

zandroid commented Jul 3, 2016

@ellbee, sure, have added two tests.

@coveralls
Copy link

coveralls commented Jul 3, 2016

Coverage Status

Coverage remained the same at 100.0% when pulling 28873e8 on zandroid:rethrow-exception into 9dd61bf on reactjs:master.

@ellbee
Copy link
Collaborator

ellbee commented Jul 3, 2016

Thanks @zandroid.

I've checked the test out, but I'm not sure I fully understand the problem this is trying to fix. Is it that it doesn't throw an exception the second time it receives the same set of arguments consecutively? What problem does that cause you?

You mention above the return value being null, but that is only returned when the selectors previous result was null. So either it never calculated a result without throwing (returning the default value of null) or the previous result was actually null.

@zandroid
Copy link
Contributor Author

zandroid commented Jul 3, 2016

it doesn't throw an exception the second time it receives the same set of arguments

It's exactly the issue that I have in my code. But anyway I think selector should not memoize (default or previous) result for current args when func throws exception.

There may be other issue, not for default null value, for example:

const selector = createSelector(state => state.a, (a) {
  if (a > 1) throw new Error()
  return a;
})
selector({ a: 1 }) // => 1 - OK
try {
  selector({ a: 2 }) // => throw error - As expected
} catch (e) {}
selector({ a: 2 }) // => 1 - What?!

@ellbee
Copy link
Collaborator

ellbee commented Jul 3, 2016

Ok, thanks! So the problem is that it doesn't throw the second time—the return value being null is not relevant. It seems strange to have your selectors throwing errors, but I think I agree that the behaviour in this PR makes more sense.

@ellbee ellbee merged commit 3bc865b into reduxjs:master Jul 3, 2016
@ellbee
Copy link
Collaborator

ellbee commented Jul 3, 2016

@zandroid Released in v2.5.2. Thanks for the PR!

@zandroid
Copy link
Contributor Author

zandroid commented Jul 3, 2016

A lot of thanks. I agree that throwing exceptions from selector is not usual case, but we use it in our selectors to detect incorrect id of requested entity and show 404, 403 or 401 pages.

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

Successfully merging this pull request may close these issues.

4 participants