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

Safe Apollo Reducers that can't crash the store #874

Merged
merged 7 commits into from
Dec 1, 2016
Merged

Conversation

rricard
Copy link
Contributor

@rricard rricard commented Nov 5, 2016

TODO:

  • If this PR is a new feature, reference an issue where a consensus about the design was reached (not necessary for small changes)
  • Make sure all of the significant new logic is covered by tests
  • Rebase your changes on master so that they can be merged easily
  • Make sure all tests and linter rules pass
  • Update CHANGELOG.md with your change
  • Add your name and email to the AUTHORS file (optional)
  • If this was a change that affects the external API, update the docs and post a link to the PR in the discussion

We prevent throwing inside the reducer by wrapping it in a try/catch block. The error will still be reported to the user.

rricard added a commit that referenced this pull request Nov 5, 2016
@rricard
Copy link
Contributor Author

rricard commented Nov 5, 2016

@helfer here it is finally!

@rricard rricard added the ready label Nov 5, 2016
@rricard rricard self-assigned this Nov 5, 2016
Copy link
Contributor

@helfer helfer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is neat, thanks a lot! Do you think you could come up with a test that breaks the resolvers and exercises the code in the catch block? If not, I think we should add a console.warn statement so that it's possible to debug these errors (otherwise they will essentially be silently swallowed).

It would also be useful to have at least one example of something that this code helps fix in the wild. I know it's good for stability, but we also have to worry about debuggability, and right now I'm leaning a bit more towards debuggability.

@helfer
Copy link
Contributor

helfer commented Nov 17, 2016

@rricard let me know if you have time to take another look at this. I would really like to merge it for the 0.6 release which will be in the next few days.

@rricard
Copy link
Contributor Author

rricard commented Nov 17, 2016

@helfer can that wait the week end ? or I can try to take a look tonight if 0.6 is really coming soon ...

@rricard
Copy link
Contributor Author

rricard commented Nov 17, 2016

But I agree, I have to find some way to ensure it will not swallow errors

@helfer
Copy link
Contributor

helfer commented Nov 17, 2016

@rricard by the weekend is fine, no rush!

We prevent throwing inside the reducer by wrapping it in a try/catch block. The error will still be reported to the user.
Ensure the error is catched and not swallowed
Ensure redux is able to carry on after the error
@rricard
Copy link
Contributor Author

rricard commented Nov 20, 2016

@helfer: so the main uses I had of this catcher are now gone since the legacy diffing is gone. So I had to find an another way to trigger a throw in the reducer. What I test here works, moreover I ensure the error is not swallowed.

@rricard
Copy link
Contributor Author

rricard commented Nov 20, 2016

So for me this error catcher shouldn't be triggered once but when it does, that means something is going wrong in Apollo, an error is likely to be uncatched. And in this case, even if something is really wrong, that doesn't mean we should let redux crash. So I would be for adding a big warning in the console printing the stacktrace asking for the user to open up a bug report about it if the reducer crashes. Note that a reducer crash can happen if we throw (what the legacy query diffing was doing) or if we end up hitting an undefined prop. Both situations need to be catched and that's we're doing here!

@helfer
Copy link
Contributor

helfer commented Nov 29, 2016

@rricard awesome! Once you add the warning, we're good to merge here.

@helfer
Copy link
Contributor

helfer commented Dec 1, 2016

@rricard I just tried adding a warning, which made me realize that errors that are not Apollo Client internal errors are actually possible in the reducers, which means that the error is more likely due to code that the user wrote than due to code that we wrote. Concrete examples:

  • dataIdFromObject that starts with a $ character.
  • replacing a real id node in the store with a generated id node

For that reason I will not add a warning and simply merge the PR as it is now 🎉

@helfer helfer merged commit a3128d6 into master Dec 1, 2016
@helfer helfer deleted the safe-redux branch December 1, 2016 15:53
@helfer helfer removed the ready label Dec 1, 2016
@rricard
Copy link
Contributor Author

rricard commented Dec 1, 2016

@helfer oh ok, didn't see this one but you're right! Thanks! I was going to handle that this WE but glad you took a look before ;)

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 2, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants