0

I have a question regarding the Koin handling exception.

In a module where I defined EncryptedSharedPreferences.create(), sometimes causing an exception, which results in a crash in the user's device. I see in the crashlytic report that the crash already happens over 200 times. More specific exception is below.

Caused by java.security.UnrecoverableKeyException: Failed to obtain information about key

...

Caused by android.security.KeyStoreException: -49

Apparently, this issue is already reported, yet there's no valid answer to handle or solving it. Because I'm using Koin to define the EncryptedSharedPreferences, I thought it's better to handle it inside single{} function. Put a try catch that will catch the exception. But when I try to throw an Exception inside try, it didn't catch the Exception, it's just crash, not what I expecting. The code looks like below

single(named(ENCRYPTED_SHARED_PREF)) {
    val context = androidApplication().applicationContext
    try {
        val masterKey =
            MasterKey.Builder(context, MasterKey.DEFAULT_MASTER_KEY_ALIAS)
                .setKeyScheme(MasterKey.KeyScheme.AES256_GCM)
                .build()

        EncryptedSharedPreferences.create(
            context,
            SP_ENCRYPTED_KEY,
            masterKey,
            EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
            EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
     )

        throwError()
     } catch (e: Exception) {
        Timber.d("[SHARED PREF] Terjadi exception")
        when (e) {
            is KeyStoreException -> {
               Toast.makeText(context, "Terjadi exception", Toast.LENGTH_SHORT).show()
            }
        }
        //Update missing link below
        androidApplication().getSharedPreferences(SP_KEY, Context.MODE_PRIVATE)
     }
}

...

@Throws(KeyStoreException::class)
fun throwError(): Nothing {
    throw KeyStoreException("Terjadi exception")
}

Now for the question, is there's a way to handle this kind of Exception inside Koin single or not? I had try to search for the documentation and google it, seems a dead end to me.

Other useful information:

  • EncryptedSharedPreferences version : androidx.security:security-crypto:1.1.0-alpha03
  • Issue tracker for EncryptedSharedPreferences I faced : https://issuetracker.google.com/u/1/issues/167977579
  • The crash or exception not related to Koin issue, rather it's on EncryptedSharedPreferences
  • Koin version : org.koin:koin-android-viewmodel:2.1.6

UPDATE SOLVE

Took me a while to realize that the catch block needs to return something such as a value or another object, let say normally shared preference. Nothing wrong with the code thought after in-depth research. Thanks all, I also update the code in my explanation above.

bobby I.
  • 101
  • 1
  • 11
  • I do not think this has anything to do with koin. And you should not handle it within koins dependency declaration `single {}`. There is a reason for this exception. Maybe this will help you: https://stackoverflow.com/questions/36652675/java-security-unrecoverablekeyexception-failed-to-obtain-information-about-priv – ChristianB Dec 28 '20 at 08:52
  • A `single` needs to return a value (or throw) so make sure your recovery code works that way, too. – laalto Dec 28 '20 at 09:20
  • I had seen the link you gave. And it's different from my issue. My issue is related to the lib itself, so there's nothing I can do thought. @ChristianB – bobby I. Dec 30 '20 at 01:48
  • Yaps, that's true, I guess that's what I'm missing, when I return an object, let say SharedPreference, it's work normal, thanks @laalto – bobby I. Dec 30 '20 at 01:50

1 Answers1

0

if your try - catch is not working and your app crashes , it means that the things inside the try block are in another thread . check this i had the same problem , but not inside koin init . i fixed by using android coroutine -> i can catch the exception now

you can also check for async variables inside koin here

Arvin Rezaei
  • 818
  • 10
  • 25
  • I think the issue not because it's in another thread, rather I do not return value in the ```catch``` block, trying hard to realize that. really appreciate your answer, thanks – bobby I. Dec 30 '20 at 01:52