mirror of
https://github.com/corda/corda.git
synced 2024-12-21 22:07:55 +00:00
Unwrapping InvocationTargetException during config parsing (#2811)
This commit is contained in:
parent
932d632716
commit
9afcbb16a2
@ -9,6 +9,7 @@ import net.corda.core.internal.uncheckedCast
|
||||
import net.corda.core.utilities.NetworkHostAndPort
|
||||
import org.slf4j.LoggerFactory
|
||||
import java.lang.reflect.Field
|
||||
import java.lang.reflect.InvocationTargetException
|
||||
import java.lang.reflect.Modifier.isStatic
|
||||
import java.lang.reflect.ParameterizedType
|
||||
import java.net.Proxy
|
||||
@ -62,19 +63,20 @@ fun <T : Any> Config.parseAs(clazz: KClass<T>): T {
|
||||
val path = defaultToOldPath(property)
|
||||
getValueInternal<Any>(path, param.type)
|
||||
}
|
||||
return constructor.callBy(args)
|
||||
try {
|
||||
return constructor.callBy(args)
|
||||
} catch (e: InvocationTargetException) {
|
||||
throw e.cause!!
|
||||
}
|
||||
}
|
||||
|
||||
class UnknownConfigurationKeysException private constructor(val unknownKeys: Set<String>) : IllegalArgumentException(message(unknownKeys)) {
|
||||
|
||||
init {
|
||||
require(unknownKeys.isNotEmpty()) { "Absence of unknown keys should not raise UnknownConfigurationKeysException." }
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
||||
fun of(offendingKeys: Set<String>): UnknownConfigurationKeysException = UnknownConfigurationKeysException(offendingKeys)
|
||||
|
||||
private fun message(offendingKeys: Set<String>) = "Unknown configuration keys: ${offendingKeys.joinToString(", ", "[", "]")}."
|
||||
}
|
||||
}
|
||||
|
@ -7,8 +7,7 @@ import com.typesafe.config.ConfigValueFactory
|
||||
import net.corda.core.identity.CordaX500Name
|
||||
import net.corda.core.internal.div
|
||||
import net.corda.core.utilities.NetworkHostAndPort
|
||||
import org.assertj.core.api.Assertions.assertThat
|
||||
import org.assertj.core.api.Assertions.assertThatThrownBy
|
||||
import org.assertj.core.api.Assertions.*
|
||||
import org.junit.Test
|
||||
import java.net.URL
|
||||
import java.nio.file.Path
|
||||
@ -188,6 +187,14 @@ class ConfigParsingTest {
|
||||
assertThat(NullableData(null).toConfig()).isEqualTo(empty())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `data class with checks`() {
|
||||
val config = config("positive" to -1)
|
||||
assertThatExceptionOfType(IllegalArgumentException::class.java)
|
||||
.isThrownBy { config.parseAs<PositiveData>() }
|
||||
.withMessageContaining("-1")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `old config property`() {
|
||||
assertThat(config("oldValue" to "old").parseAs<OldData>().newValue).isEqualTo("old")
|
||||
@ -291,6 +298,11 @@ class ConfigParsingTest {
|
||||
data class DataListData(val list: List<StringData>)
|
||||
data class DefaultData(val a: Int, val defaultOfTwo: Int = 2)
|
||||
data class NullableData(val nullable: String?)
|
||||
data class PositiveData(private val positive: Int) {
|
||||
init {
|
||||
require(positive > 0) { "$positive is not positive" }
|
||||
}
|
||||
}
|
||||
data class OldData(
|
||||
@OldConfig("oldValue")
|
||||
val newValue: String)
|
||||
|
Loading…
Reference in New Issue
Block a user