InsertKoinIO / koin

Koin - a pragmatic lightweight dependency injection framework for Kotlin & Kotlin Multiplatform
https://insert-koin.io
Apache License 2.0
9.09k stars 719 forks source link

`androidContext()` swallows/hides underlying exceptions #1932

Closed yogurtearl closed 2 months ago

yogurtearl commented 3 months ago

If the get() for a context throws an exception, the cause is discarded. (same applies for Scope.androidApplication())

The e here is discarded.

See this code: https://github.com/InsertKoinIO/koin/blob/5fab1ca74add39339e281aaa822c618a2da5baa3/projects/android/koin-android/src/main/java/org/koin/android/ext/koin/ModuleExt.kt#L30-L34

Instead it should be something like this:

class MissingAndroidContextException(message: String, cause: kotlin.Throwable?)
     : kotlin.Exception(message, cause)

fun Scope.androidContext(): Context = try {
    get()
} catch (e: Exception) {
    throw MissingAndroidContextException("Can't resolve Context instance. $ERROR_MSG", e)
}
arnaudgiuliani commented 2 months ago

Thanks