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

Exception handling when reading datastore. #1710

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

nur-shuvo
Copy link

What I have done and why

Datastore file can be corrupted in cases, or simply invalid. So there needs to handle runtime Fatal exception like I found:

FATAL EXCEPTION: main
Process: com.google.samples.apps.nowinandroid.demo.debug, PID: 10565
androidx.datastore.core.CorruptionException: Cannot read proto.
...

So exception handling is needed, and replaced with UserPreferences.getDefaultInstance() when there occurs such exception

@SimonMarquis
Copy link
Contributor

It would be great to add tests for this new code.
Also, using the Android Log here means we won't be able to run standard JVM unit test 😕

@nur-shuvo
Copy link
Author

It would be great to add tests for this new code. Also, using the Android Log here means we won't be able to run standard JVM unit test 😕

I will try to add tests, though it seems that corrupting the file in unit test environment is challenging, maybe I might have to write androidTest. I will look into this. Thank you for your review. @SimonMarquis

@SimonMarquis
Copy link
Contributor

I'm not sure exactly how for now but I think you can fake the datastore and throw the IOException from the loading method.

Copy link
Contributor

@hoc081098 hoc081098 left a comment

Choose a reason for hiding this comment

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

Nice :shipit:

@nur-shuvo
Copy link
Author

I'm not sure exactly how for now but I think you can fake the datastore and throw the IOException from the loading method.

Added unit tests by making fake datastore which throws exception while accessing. Then testing data source whether returning the default values or not.

@nur-shuvo nur-shuvo requested a review from hoc081098 December 10, 2024 15:48
Copy link
Contributor

@hoc081098 hoc081098 left a comment

Choose a reason for hiding this comment

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

LGTM :shipit:

@nur-shuvo
Copy link
Author

@dturner
Hello.
Could you please review this PR at your convenient?

@dturner
Copy link
Collaborator

dturner commented Dec 19, 2024

If datastore isn't available, returning a default UserPreferences isn't the answer. The whole point of the app is that users can save the topics they're interested in and only see news relating to those topics (plus bookmarks etc). Without working data storage, none of that is possible. It's preferable to have the app crash on startup than have users be fooled into thinking it's working when it's not.

What am I missing?

@dturner dturner requested review from dturner and removed request for SimonMarquis December 19, 2024 14:34
@nur-shuvo
Copy link
Author

If datastore isn't available, returning a default UserPreferences isn't the answer. The whole point of the app is that users can save the topics they're interested in and only see news relating to those topics (plus bookmarks etc). Without working data storage, none of that is possible. It's preferable to have the app crash on startup than have users be fooled into thinking it's working when it's not.

What am I missing?

I think application will continuously crash on startUp if this exception unhandled. User may need to clear data forcefully, or uninstalled to recover from this unexpected situation. That's why I put the default UserPreferences and restored to something where user can fresh start using the app.

@SimonMarquis
Copy link
Contributor

@dturner it is not necessarily that "datastore" is not available, but rather that the datastore files are corrupted, and not recoverable.
You can take a look at this report https://issuetracker.google.com/issues/346197747 that hit many of us, for unknown reasons. The only resort for users is to clear app data, or reinstall app, which would be equivalent to restoring an empty datastore file. We will lose the user data anyways.

@nur-shuvo
Copy link
Author

@dturner
I would be happy to know your opinions regarding.

@dturner
Copy link
Collaborator

dturner commented Dec 24, 2024

Thanks for the additional info, that makes sense. I'll need some time to consider the suggested approach before providing a review. Unfortunately I'm on vacation for a couple of weeks now so it'll likely be mid Jan 2025 now.

@nur-shuvo nur-shuvo changed the title Exception handling for when reading datastore. Exception handling when reading datastore. Dec 28, 2024
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