Unwrapping InvocationTargetException during config parsing (#2811)

This commit is contained in:
Shams Asari 2018-03-14 07:04:25 +00:00 committed by GitHub
parent 932d632716
commit 9afcbb16a2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 6 deletions

View File

@ -9,6 +9,7 @@ import net.corda.core.internal.uncheckedCast
import net.corda.core.utilities.NetworkHostAndPort import net.corda.core.utilities.NetworkHostAndPort
import org.slf4j.LoggerFactory import org.slf4j.LoggerFactory
import java.lang.reflect.Field import java.lang.reflect.Field
import java.lang.reflect.InvocationTargetException
import java.lang.reflect.Modifier.isStatic import java.lang.reflect.Modifier.isStatic
import java.lang.reflect.ParameterizedType import java.lang.reflect.ParameterizedType
import java.net.Proxy import java.net.Proxy
@ -62,19 +63,20 @@ fun <T : Any> Config.parseAs(clazz: KClass<T>): T {
val path = defaultToOldPath(property) val path = defaultToOldPath(property)
getValueInternal<Any>(path, param.type) 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)) { class UnknownConfigurationKeysException private constructor(val unknownKeys: Set<String>) : IllegalArgumentException(message(unknownKeys)) {
init { init {
require(unknownKeys.isNotEmpty()) { "Absence of unknown keys should not raise UnknownConfigurationKeysException." } require(unknownKeys.isNotEmpty()) { "Absence of unknown keys should not raise UnknownConfigurationKeysException." }
} }
companion object { companion object {
fun of(offendingKeys: Set<String>): UnknownConfigurationKeysException = UnknownConfigurationKeysException(offendingKeys) fun of(offendingKeys: Set<String>): UnknownConfigurationKeysException = UnknownConfigurationKeysException(offendingKeys)
private fun message(offendingKeys: Set<String>) = "Unknown configuration keys: ${offendingKeys.joinToString(", ", "[", "]")}." private fun message(offendingKeys: Set<String>) = "Unknown configuration keys: ${offendingKeys.joinToString(", ", "[", "]")}."
} }
} }

View File

@ -7,8 +7,7 @@ import com.typesafe.config.ConfigValueFactory
import net.corda.core.identity.CordaX500Name import net.corda.core.identity.CordaX500Name
import net.corda.core.internal.div import net.corda.core.internal.div
import net.corda.core.utilities.NetworkHostAndPort import net.corda.core.utilities.NetworkHostAndPort
import org.assertj.core.api.Assertions.assertThat import org.assertj.core.api.Assertions.*
import org.assertj.core.api.Assertions.assertThatThrownBy
import org.junit.Test import org.junit.Test
import java.net.URL import java.net.URL
import java.nio.file.Path import java.nio.file.Path
@ -188,6 +187,14 @@ class ConfigParsingTest {
assertThat(NullableData(null).toConfig()).isEqualTo(empty()) 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 @Test
fun `old config property`() { fun `old config property`() {
assertThat(config("oldValue" to "old").parseAs<OldData>().newValue).isEqualTo("old") 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 DataListData(val list: List<StringData>)
data class DefaultData(val a: Int, val defaultOfTwo: Int = 2) data class DefaultData(val a: Int, val defaultOfTwo: Int = 2)
data class NullableData(val nullable: String?) data class NullableData(val nullable: String?)
data class PositiveData(private val positive: Int) {
init {
require(positive > 0) { "$positive is not positive" }
}
}
data class OldData( data class OldData(
@OldConfig("oldValue") @OldConfig("oldValue")
val newValue: String) val newValue: String)