Skip to content

Commit c357e1c

Browse files
committed
Provide better error message
Provide better error message on failure to construct classes.
1 parent 24f252b commit c357e1c

2 files changed

Lines changed: 33 additions & 9 deletions

File tree

lib/src/main/kotlin/io/nexure/capsule/Capsule.kt

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,13 @@ private constructor(
8383
): Comparable<Dependency> {
8484
private val instance: LazyValue<Any> = LazyValue(constructor)
8585

86-
fun getInstance(): Any = instance()
86+
fun getInstance(): Any {
87+
return try {
88+
instance()
89+
} catch (e: Exception) {
90+
throw DependencyException(key, e)
91+
}
92+
}
8793

8894
override fun toString(): String = key
8995

@@ -99,12 +105,11 @@ private constructor(
99105
private fun classKey(clazz: Class<*>): String = clazz.canonicalName ?: clazz.descriptorString()
100106

101107
internal class DependencyException(
102-
val clazz: Class<*>,
103-
override val cause: DependencyException? = null
108+
val key: String,
109+
override val cause: Exception? = null
104110
) : Exception() {
105-
override val message: String = if (cause != null) {
106-
"Unable to provide dependency for class $clazz: \n\t${cause.message}"
107-
} else {
108-
"Unable to provide dependency for class $clazz"
109-
}
111+
constructor(clazz: Class<*>, cause: Exception? = null) : this(classKey(clazz), cause)
112+
113+
override val message: String = "Unable to provide dependency for class ${rootKey()}"
114+
fun rootKey(): String = if (this.cause is DependencyException) this.cause.key else this.key
110115
}

lib/src/test/kotlin/io/nexure/capsule/CapsuleTest.kt

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package io.nexure.capsule
33
import org.junit.Test
44
import kotlin.test.assertEquals
55
import kotlin.test.assertNull
6+
import kotlin.test.assertTrue
67

78
class CapsuleTest {
89
@Test
@@ -162,7 +163,6 @@ class CapsuleTest {
162163
assertEquals(listOf("3", "2", "1"), third.getMany<Greeter>().map { it.hello() })
163164
}
164165

165-
166166
@Test
167167
fun `test only needed dependency in created on call to get()`() {
168168
val parent = Capsule {
@@ -182,6 +182,25 @@ class CapsuleTest {
182182
val greeter: Greeter = child.get()
183183
assertEquals("foo", greeter.hello())
184184
}
185+
186+
@Test
187+
fun `DependencyException should provide context on which class creation that failed`() {
188+
class ComplexGreeter(
189+
val string: String,
190+
val double: Double
191+
) : Greeter {
192+
override fun hello(): String = "foo"
193+
}
194+
195+
val capsule = Capsule {}
196+
try {
197+
val g = capsule.get<ComplexGreeter>()
198+
assertTrue(false)
199+
} catch (e: DependencyException) {
200+
assertEquals("double", e.rootKey())
201+
assertEquals("Unable to provide dependency for class double", e.message)
202+
}
203+
}
185204
}
186205

187206
interface Greeter {

0 commit comments

Comments
 (0)