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

handle useLoadData synchronous exception edge case #13

Merged
merged 1 commit into from
Feb 3, 2024

Conversation

nthurow
Copy link
Contributor

@nthurow nthurow commented Feb 2, 2024

Proposed changes

If the fetchData function passed to useLoadData throws a synchronous exception, then useLoadData should handle that and should return an initial state of "error". Previously, useLoadData was not handling this scenario and a fetchData function that threw a synchronous exception would result in an unhandled exception.

For example, the following scenario currently throws an unhandled exception:

useLoadData(() => {
  throw 'an error';
});

This PR allows the above scenario to be correctly handled such that useLoadData would simply return:

{
  inProgress: false,
  result: undefined,
  isError: true,
  error: 'an error'
}

Types of changes

What types of changes does your code introduce?
Put an x in the boxes that apply

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

Checklist

Put an x in the boxes that apply. You can also fill these out after creating the PR. If you're unsure about any of them, don't hesitate to ask. We're here to help! This is simply a reminder of what we are going to look for before merging your code.

  • I have read the CONTRIBUTING doc
  • I have added tests that prove my fix is effective or that my feature works
  • I have added necessary documentation (if appropriate)
  • Any dependent changes have been merged and published in downstream modules

Further comments

If the `fetchData` function passed to `useLoadData` throws a
synchronous exception, then `useLoadData` should handle that and should
return an initial state of "error".  Previously, `useLoadData` was not
handling this scenario and a `fetchData` function that threw a
synchronous exception would result in an unhandled exception.
Comment on lines +185 to +190
// Initialize our pending data to one of three possible states:
// 1. If initial data was supplied or if the fetchData function returned a non-Promise value,
// then our initial state will be already "resolved" (not in-progress and not error, we already have the result)
// 2. If initial data was not supplied and fetchData returned a Promise, then our initial state is in-progress
// 3. If initial data was not supplied and fetchData threw a *synchronous* (non-Promise) exception,
// then our initial state is "rejected" (not in-progress and already has an error value)
Copy link
Contributor

Choose a reason for hiding this comment

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

I will definitely look into adding more comments throughout this code like this

Copy link
Contributor

@NielsJPeschel NielsJPeschel left a comment

Choose a reason for hiding this comment

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

Awesome PR!

@NielsJPeschel NielsJPeschel merged commit 08e5dca into main Feb 3, 2024
2 checks passed
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.

2 participants