[#1372] Throw if parsing configuration failed

- Closes #1372
- Follow up #1373
This commit is contained in:
Honza Rychnovský 2024-04-23 08:43:14 +02:00 committed by GitHub
parent 786c8c2cab
commit ff2ce3a379
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 45 additions and 20 deletions

View File

@ -40,6 +40,8 @@ private data class MergingConfiguration(private val configurations: PersistentLi
return null != configurations.firstWithKey(key) return null != configurations.firstWithKey(key)
} }
// TODO [#1373]: Catch and log Configuration Key Coercion Failures
// TODO [#1373]: https://github.com/Electric-Coin-Company/zashi-android/issues/1373
override fun getBoolean( override fun getBoolean(
key: ConfigKey, key: ConfigKey,
defaultValue: Boolean defaultValue: Boolean
@ -49,6 +51,8 @@ private data class MergingConfiguration(private val configurations: PersistentLi
} ?: defaultValue } ?: defaultValue
} }
// TODO [#1373]: Catch and log Configuration Key Coercion Failures
// TODO [#1373]: https://github.com/Electric-Coin-Company/zashi-android/issues/1373
override fun getInt( override fun getInt(
key: ConfigKey, key: ConfigKey,
defaultValue: Int defaultValue: Int

View File

@ -6,5 +6,7 @@ data class BooleanConfigurationEntry(
override val key: ConfigKey, override val key: ConfigKey,
private val defaultValue: Boolean private val defaultValue: Boolean
) : DefaultEntry<Boolean> { ) : DefaultEntry<Boolean> {
// TODO [#1373]: Catch and log Configuration Key Coercion Failures
// TODO [#1373]: https://github.com/Electric-Coin-Company/zashi-android/issues/1373
override fun getValue(configuration: Configuration) = configuration.getBoolean(key, defaultValue) override fun getValue(configuration: Configuration) = configuration.getBoolean(key, defaultValue)
} }

View File

@ -6,5 +6,7 @@ data class IntegerConfigurationEntry(
override val key: ConfigKey, override val key: ConfigKey,
private val defaultValue: Int private val defaultValue: Int
) : DefaultEntry<Int> { ) : DefaultEntry<Int> {
// TODO [#1373]: Catch and log Configuration Key Coercion Failures
// TODO [#1373]: https://github.com/Electric-Coin-Company/zashi-android/issues/1373
override fun getValue(configuration: Configuration) = configuration.getInt(key, defaultValue) override fun getValue(configuration: Configuration) = configuration.getInt(key, defaultValue)
} }

View File

@ -0,0 +1,7 @@
package co.electriccoin.zcash.configuration.model.exception
/**
* Exception that may occur when parsing a value from the remote configuration. This could mean that someone made an
* error in the remote config console.
*/
class ConfigurationParseException(message: String, cause: Throwable?) : IllegalArgumentException(message, cause)

View File

@ -1,6 +1,7 @@
package co.electriccoin.zcash.configuration.model.map package co.electriccoin.zcash.configuration.model.map
import co.electriccoin.zcash.configuration.model.entry.ConfigKey import co.electriccoin.zcash.configuration.model.entry.ConfigKey
import co.electriccoin.zcash.configuration.model.exception.ConfigurationParseException
import kotlinx.collections.immutable.PersistentMap import kotlinx.collections.immutable.PersistentMap
import kotlinx.datetime.Instant import kotlinx.datetime.Instant
@ -10,31 +11,35 @@ data class StringConfiguration(
val configurationMapping: PersistentMap<String, String>, val configurationMapping: PersistentMap<String, String>,
override val updatedAt: Instant? override val updatedAt: Instant?
) : Configuration { ) : Configuration {
@Throws(ConfigurationParseException::class)
override fun getBoolean( override fun getBoolean(
key: ConfigKey, key: ConfigKey,
defaultValue: Boolean defaultValue: Boolean
) = configurationMapping[key.key]?.let { ) = configurationMapping[key.key]?.let {
try { try {
it.toBooleanStrict() it.toBooleanStrict()
} catch ( } catch (e: IllegalArgumentException) {
@Suppress("SwallowedException") e: IllegalArgumentException throw ConfigurationParseException(
) { "Failed while parsing String value to Boolean. This could mean " +
// In the future, log coercion failure as this could mean someone made an error in the remote config console "someone made an error in the remote config console",
defaultValue e
)
} }
} ?: defaultValue } ?: defaultValue
@Throws(ConfigurationParseException::class)
override fun getInt( override fun getInt(
key: ConfigKey, key: ConfigKey,
defaultValue: Int defaultValue: Int
) = configurationMapping[key.key]?.let { ) = configurationMapping[key.key]?.let {
try { try {
it.toInt() it.toInt()
} catch ( } catch (e: IllegalArgumentException) {
@Suppress("SwallowedException") e: NumberFormatException throw ConfigurationParseException(
) { "Failed while parsing String value to Int. This could mean " +
// In the future, log coercion failure as this could mean someone made an error in the remote config console "someone made an error in the remote config console",
defaultValue e
)
} }
} ?: defaultValue } ?: defaultValue

View File

@ -1,6 +1,7 @@
package co.electriccoin.zcash.configuration.test package co.electriccoin.zcash.configuration.test
import co.electriccoin.zcash.configuration.model.entry.ConfigKey import co.electriccoin.zcash.configuration.model.entry.ConfigKey
import co.electriccoin.zcash.configuration.model.exception.ConfigurationParseException
import co.electriccoin.zcash.configuration.model.map.Configuration import co.electriccoin.zcash.configuration.model.map.Configuration
import kotlinx.datetime.Instant import kotlinx.datetime.Instant
@ -13,31 +14,35 @@ import kotlinx.datetime.Instant
class MockConfiguration(private val configurationMapping: Map<String, String> = emptyMap()) : Configuration { class MockConfiguration(private val configurationMapping: Map<String, String> = emptyMap()) : Configuration {
override val updatedAt: Instant? = null override val updatedAt: Instant? = null
@Throws(ConfigurationParseException::class)
override fun getBoolean( override fun getBoolean(
key: ConfigKey, key: ConfigKey,
defaultValue: Boolean defaultValue: Boolean
) = configurationMapping[key.key]?.let { ) = configurationMapping[key.key]?.let {
try { try {
it.toBooleanStrict() it.toBooleanStrict()
} catch ( } catch (e: IllegalArgumentException) {
@Suppress("SwallowedException") e: IllegalArgumentException throw ConfigurationParseException(
) { "Failed while parsing String value to Boolean. This could mean " +
// In the future, log coercion failure as this could mean someone made an error in the remote config console "someone made an error in the remote config console",
defaultValue e
)
} }
} ?: defaultValue } ?: defaultValue
@Throws(ConfigurationParseException::class)
override fun getInt( override fun getInt(
key: ConfigKey, key: ConfigKey,
defaultValue: Int defaultValue: Int
) = configurationMapping[key.key]?.let { ) = configurationMapping[key.key]?.let {
try { try {
it.toInt() it.toInt()
} catch ( } catch (e: IllegalArgumentException) {
@Suppress("SwallowedException") e: NumberFormatException throw ConfigurationParseException(
) { "Failed while parsing String value to Int. This could mean " +
// In the future, log coercion failure as this could mean someone made an error in the remote config console "someone made an error in the remote config console",
defaultValue e
)
} }
} ?: defaultValue } ?: defaultValue