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

Fix sync state exception handling #1669

Closed
wants to merge 18 commits into from

Conversation

ellykits
Copy link
Collaborator

@ellykits ellykits commented Oct 17, 2022

IMPORTANT: All PRs must be linked to an issue (except for extremely trivial and straightforward changes).

Fixes #1654

Description
Surround the call site of com.google.android.fhir.impl.FhirEngineImpl#saveRemoteResourcesToDatabase and com.google.android.fhir.impl.FhirEngineImpl#saveResolvedResourcesToDatabase in try-catch block.

Alternative(s) considered
The exception we were getting was a NullPointerException

java.lang.NullPointerException: coding.code must not be null
2022-10-18 10:38:10.162  7941-7985  FhirEngine...ed$collect org.smartregister.fhircore.map       E  	at com.google.android.fhir.index.ResourceIndexer.tokenIndex(ResourceIndexer.kt:319)
2022-10-18 10:38:10.162  7941-7985  FhirEngine...ed$collect org.smartregister.fhircore.map       E  	at com.google.android.fhir.index.ResourceIndexer.extractIndexValues(ResourceIndexer.kt:92)
2022-10-18 10:38:10.162  7941-7985  FhirEngine...ed$collect org.smartregister.fhircore.map       E  	at com.google.android.fhir.index.ResourceIndexer.index(ResourceIndexer.kt:71)
2022-10-18 10:38:10.162  7941-7985  FhirEngine...ed$collect org.smartregister.fhircore.map       E  	at com.google.android.fhir.db.impl.dao.ResourceDao.insertResource(ResourceDao.kt:194)

Solution would be to add the null check before using the property on this line com/google/android/fhir/index/ResourceIndexer.kt:317. This will however only address this type of exception and ignore any other exceptions that may happen.

Type
Choose one: Bug fix

Checklist

  • I have read and acknowledged the Code of conduct.
  • I have read the Contributing page.
  • I have signed the Google Individual CLA, or I am covered by my company's Corporate CLA.
  • I have discussed my proposed solution with code owners in the linked issue(s) and we have agreed upon the general approach.
  • I have run ./gradlew spotlessApply and ./gradlew spotlessCheck to check my code follows the style guide of this project.
  • I have run ./gradlew check and ./gradlew connectedCheck to test my changes locally.
  • I have built and run the demo app(s) to verify my change fixes the issue and/or does not break the demo app(s).

Remove state emission from catch section of try-catch block. This
violates flow exception handling.

Signed-off-by: Elly Kitoto <junkmailstoelly@gmail.com>
When an exception occurs while attempting to save a resource,
the rest of the process should not be affected.

Signed-off-by: Elly Kitoto <junkmailstoelly@gmail.com>
Signed-off-by: Elly Kitoto <junkmailstoelly@gmail.com>
Signed-off-by: Elly Kitoto <junkmailstoelly@gmail.com>
@@ -53,7 +53,7 @@ internal class DownloaderImpl(
)
)
} catch (exception: Exception) {
emit(DownloadState.Failure(ResourceSyncException(resourceTypeToDownload, exception)))
Copy link
Collaborator

Choose a reason for hiding this comment

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

we may not want to remove this line. sync should emit failure when resource syncing fails.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I see. I have reverted this line so this state can be emitted.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

But this needs to be handled not only by the implementers but also in FHirEngineImpl where the download function is called.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

emitting events in catch block is however not recommended because of such unexpected behaviors when the exception is not correctly handle where we are collecting the states for the flow.

saveResolvedResourcesToDatabase(resolved)
}
} catch (exception: Exception) {
Timber.e(exception, "Save remote and resolved conflicting resources to database failed")
Copy link
Collaborator

Choose a reason for hiding this comment

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

we may not want to just catch exception for resources which have failed to save. it should either show this error to user, or it should do something to log error somewhere to resolve the issue with DB. this can lead to suppress app issues with data collection without getting noticed.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I agree we may need to do more with this error log but do we really want to show the user this error? Is there a strategy in SDK for handling/logging errors?

Signed-off-by: Elly Kitoto <junkmailstoelly@gmail.com>
@jingtang10 jingtang10 self-assigned this Dec 4, 2022
ndegwamartin added a commit to opensrp/android-fhir that referenced this pull request Feb 24, 2023
- With unmerged PR google#1669 branch
ndegwamartin added a commit to opensrp/android-fhir that referenced this pull request Mar 1, 2023
  - With unmerged PR google#1669 branch
ndegwamartin added a commit to opensrp/android-fhir that referenced this pull request Jun 26, 2023
      - With unmerged PR #1
      - With unmerged PR google#1917
      - With unmerged PR google#1978
      - With unmerged PR google#1907
      - With unmerged PR google#2016
      - With unmerged PR google#2032
      - With unmerged PR google#1669
ndegwamartin added a commit to opensrp/android-fhir that referenced this pull request Jun 26, 2023
   - With unmerged PR #1
   - With unmerged PR google#1917
   - With unmerged PR google#1978
   - With unmerged PR google#1907
   - With unmerged PR google#2032
   - With unmerged PR google#1669
   - With unmerged PR google#2047
ndegwamartin added a commit to opensrp/android-fhir that referenced this pull request Aug 23, 2023
- With unmerged PR google#2032
- With unmerged PR #1
- With unmerged PR google#1669
- With unmerged PR google#2132
- With unmerged PR #9
ndegwamartin added a commit to opensrp/android-fhir that referenced this pull request Aug 23, 2023
- With unmerged PR google#2032
- With unmerged PR #1
- With unmerged PR google#1669
- With unmerged PR google#2132
ndegwamartin added a commit to opensrp/android-fhir that referenced this pull request Aug 28, 2023
    - With unmerged PR google#2032
    - With unmerged PR #1
    - With unmerged PR google#1669
    - With unmerged PR google#2132
    - With unmerged PR #9
    - With unmerged PR google#2076
    - With unmerged PR #10
ndegwamartin added a commit to opensrp/android-fhir that referenced this pull request Sep 7, 2023
        - With unmerged PR google#2032
        - With unmerged PR #1
        - With unmerged PR google#1669
        - With unmerged PR google#2132
        - With unmerged PR #9
        - With unmerged PR google#2076
        - With unmerged PR #10
ndegwamartin added a commit to opensrp/android-fhir that referenced this pull request Sep 7, 2023
        - With unmerged PR google#2032
        - With unmerged PR #1
        - With unmerged PR google#1669
        - With unmerged PR google#2132
        - With unmerged PR #9
        - With unmerged PR google#2076
        - With unmerged PR #10
ndegwamartin added a commit to opensrp/android-fhir that referenced this pull request Sep 13, 2023
            - With unmerged PR #1
            - With unmerged PR google#1669
            - With unmerged PR #9
            - With unmerged PR google#2076
            - With unmerged PR #10
ndegwamartin added a commit to opensrp/android-fhir that referenced this pull request Sep 13, 2023
            - With unmerged PR #1
            - With unmerged PR google#1669
            - With unmerged PR #9
            - With unmerged PR #10
@jingtang10
Copy link
Collaborator

hey @ellykits - sorry for not having closed this earlier. looking at this PR again i'm not confident this is the route we want to take to fix the problem. what is done here is wrapping the problem in a try catch instead of addressing the exception. for example, if it's network issue, we might need to try again, or if this is a failure we can't recover from we need a way for the application to know about that.

additionally, @santosh-pingle is working on #2142 which is very relevant to this problem.

so i'll close this PR - but we should still keep the issue open to continue exploring the correct solution.

@jingtang10 jingtang10 closed this Sep 14, 2023
ndegwamartin added a commit to opensrp/android-fhir that referenced this pull request Sep 18, 2023
  - With unmerged PR #1
  - With unmerged PR google#1669
  - With unmerged PR google#2178
  - With unmerged PR #9
  - With unmerged PR #10
ndegwamartin added a commit to opensrp/android-fhir that referenced this pull request Sep 18, 2023
  - With unmerged PR #1
  - With unmerged PR google#1669
  - With unmerged PR google#2178
  - With unmerged PR #9
  - With unmerged PR #10
ndegwamartin added a commit to opensrp/android-fhir that referenced this pull request Sep 21, 2023
      - With unmerged PR google#1669
      - Wup google#2178
      - Wup #9
      - Wup #10
      - Wup google#2076
ndegwamartin added a commit to opensrp/android-fhir that referenced this pull request Sep 21, 2023
  - With unmerged PR #11
  - Wup google#1669
  - Wup google#2076
  - Wup #9
  - Wup google#2178
  - Wup #10
ndegwamartin added a commit to opensrp/android-fhir that referenced this pull request Oct 13, 2023
  - With unmerged PR #11
  - Wup google#1669
  - Wup #9
  - Wup google#2178
  - Wup #10
  - Wup google#2230
  - Wup google#2262
ndegwamartin added a commit to opensrp/android-fhir that referenced this pull request Oct 30, 2023
ndegwamartin added a commit to opensrp/android-fhir that referenced this pull request Oct 30, 2023
Rkareko added a commit to opensrp/android-fhir that referenced this pull request Nov 24, 2023
Rkareko added a commit to opensrp/android-fhir that referenced this pull request Nov 24, 2023
Rkareko added a commit to opensrp/android-fhir that referenced this pull request Dec 19, 2023
Rkareko added a commit to opensrp/android-fhir that referenced this pull request Jan 9, 2024
Rkareko added a commit to opensrp/android-fhir that referenced this pull request Jan 10, 2024
      - With unmerged PR #11
      - Wup google#1669
      - Wup #9
      - Wup google#2178
ndegwamartin added a commit to opensrp/android-fhir that referenced this pull request Jan 22, 2024
          - With unmerged PR google#11
          - Wup google#1669
          - Wup google#9
          - Wup google#2178
ndegwamartin added a commit to opensrp/android-fhir that referenced this pull request Jan 22, 2024
          - With unmerged PR google#11
          - Wup google#1669
          - Wup google#9
          - Wup google#2178
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.

Download fails to complete when exception is encountered
7 participants