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

Fixed: getDirectAccessInformation() method throw the InvocationTargetException and crash the application. #3898

Merged
merged 2 commits into from
Jun 26, 2024

Conversation

MohitMaliFtechiz
Copy link
Collaborator

Fixes #3897

Handling the exception thrown by the getDirectAccessInformation(). InvocationTargetException is a checked exception that wraps an exception thrown by an invoked method or constructor, and here it was thrown when constructing the DirectAccessInformation constructor. This method is used for loading media files e.g. photos, videos, etc. The reproducible steps are unknown, and there has been only 1 error in the last 60 days so it hardly occurs or occurs when an Item fails to provide the directAccessInformation. Now, we are catching that exception if thrown by this method so that it will not crash the application, and handle the loading of that media file on the Android side as shown in the below code.

 private fun loadAsset(uri: String): InputStream? {
    val article = try {
      jniKiwixReader.getEntryByPath(uri.filePath).getItem(true)
    } catch (exception: Exception) {
      Log.e(TAG, "Could not get Item for uri = $uri \n original exception = $exception")
      null
    }
    val infoPair = try {
      article?.directAccessInformation
    } catch (ignore: InvocationTargetException) {
      Log.e(
        TAG,
        "Could not get directAccessInformation for uri = $uri \n" +
          "original exception = $ignore"
      )
      null
    }
    if (infoPair == null || !File(infoPair.filename).exists()) {
      return loadAssetFromCache(uri)
    }
    return article?.size?.let {
      AssetFileDescriptor(
        infoPair.parcelFileDescriptor,
        infoPair.offset,
        it
      ).createInputStream()
    }
  }

  @Throws(IOException::class)
  private fun loadAssetFromCache(uri: String): FileInputStream {
    return File(
      FileUtils.getFileCacheDir(CoreApp.instance),
      uri.substringAfterLast("/")
    ).apply { getContent(uri)?.let(::writeBytes) }
      .inputStream()
  }

@MohitMaliFtechiz MohitMaliFtechiz marked this pull request as draft June 26, 2024 07:41
Copy link

codecov bot commented Jun 26, 2024

Codecov Report

Attention: Patch coverage is 0% with 14 lines in your changes missing coverage. Please review.

Project coverage is 53.62%. Comparing base (4a30af5) to head (352c403).

Files Patch % Lines
...org/kiwix/kiwixmobile/core/reader/ZimFileReader.kt 0.00% 14 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##               main    #3898      +/-   ##
============================================
+ Coverage     53.59%   53.62%   +0.02%     
  Complexity     1401     1401              
============================================
  Files           302      302              
  Lines         11588    11596       +8     
  Branches       1527     1528       +1     
============================================
+ Hits           6211     6218       +7     
- Misses         4378     4380       +2     
+ Partials        999      998       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@MohitMaliFtechiz MohitMaliFtechiz marked this pull request as ready for review June 26, 2024 10:39
@kelson42 kelson42 merged commit 2dd933e into main Jun 26, 2024
9 of 10 checks passed
@kelson42 kelson42 deleted the Fixes#3897 branch June 26, 2024 11:13
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.

getDirectAccessInformation() method throw the InvocationTargetException and crash the application.
3 participants