Skip to content

Commit

Permalink
Merge pull request #1970 from InsertKoinIO/fix_ctx_error
Browse files Browse the repository at this point in the history
Catch Koin injection error only - Fix #1932
  • Loading branch information
arnaudgiuliani authored Sep 9, 2024
2 parents 932afb3 + 759d27a commit 05d2a13
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package org.koin.android.ext.koin
import android.app.Application
import android.content.Context
import org.koin.android.error.MissingAndroidContextException
import org.koin.core.error.NoDefinitionFoundException
import org.koin.core.scope.Scope

const val ERROR_MSG = "Please use androidContext() function in your KoinApplication configuration."
Expand All @@ -29,7 +30,7 @@ const val ERROR_MSG = "Please use androidContext() function in your KoinApplicat
*/
fun Scope.androidContext(): Context = try {
get()
} catch (e: Exception) {
} catch (e: NoDefinitionFoundException) {
throw MissingAndroidContextException("Can't resolve Context instance. $ERROR_MSG")
}

Expand All @@ -40,6 +41,6 @@ fun Scope.androidContext(): Context = try {
*/
fun Scope.androidApplication(): Application = try {
get()
} catch (e: Exception) {
} catch (e: NoDefinitionFoundException) {
throw MissingAndroidContextException("Can't resolve Application instance. $ERROR_MSG")
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,21 @@ class ModuleExtTest {
fun `GIVEN exception WHEN ty to get android context THEN throws exception`() {
// GIVEN
val scope = mockk<Scope>(relaxed = true)
every { scope.get<Context>() } throws Exception("message")
every { scope.get<Context>() } throws MissingAndroidContextException("message")

try {
// WHEN
scope.androidContext()
} catch (e: MissingAndroidContextException) {
// THEN
Assert.assertEquals("Can't resolve Context instance. Please use androidContext() function in your KoinApplication configuration.", e.localizedMessage)
Assert.assertEquals("message", e.localizedMessage)
}
}

@Test(expected = MissingAndroidContextException::class)
fun `GIVEN exception WHEN ty to get android application THEN throws exception`() {
val scope = mockk<Scope>(relaxed = true)
every { scope.get<Application>() } throws Exception()
every { scope.get<Application>() } throws MissingAndroidContextException("")
scope.androidApplication()
}

Expand Down

0 comments on commit 05d2a13

Please sign in to comment.