[#1372] Throw if parsing configuration failed

- Closes #1372
- Follow up #1373
This commit is contained in:
Honza 2024-04-19 14:56:30 +02:00
parent 533f335038
commit ba20858d23
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)
}
// TODO [#1373]: Catch and log Configuration Key Coercion Failures
// TODO [#1373]: https://github.com/Electric-Coin-Company/zashi-android/issues/1373
override fun getBoolean(
key: ConfigKey,
defaultValue: Boolean
@ -49,6 +51,8 @@ private data class MergingConfiguration(private val configurations: PersistentLi
} ?: 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(
key: ConfigKey,
defaultValue: Int

View File

@ -6,5 +6,7 @@ data class BooleanConfigurationEntry(
override val key: ConfigKey,
private val defaultValue: 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)
}

View File

@ -6,5 +6,7 @@ data class IntegerConfigurationEntry(
override val key: ConfigKey,
private val defaultValue: 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)
}

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

View File

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